xref: /openbmc/linux/arch/alpha/kernel/perf_event.c (revision 2999a4b354c24985268f9310bc9522ff358453a8)
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>
17004417a6SPeter Zijlstra #include <linux/init.h>
18979f8671SMichael Cree 
19979f8671SMichael Cree #include <asm/hwrpb.h>
2060063497SArun Sharma #include <linux/atomic.h>
21979f8671SMichael Cree #include <asm/irq.h>
22979f8671SMichael Cree #include <asm/irq_regs.h>
23979f8671SMichael Cree #include <asm/pal.h>
24979f8671SMichael Cree #include <asm/wrperfmon.h>
25979f8671SMichael Cree #include <asm/hw_irq.h>
26979f8671SMichael Cree 
27979f8671SMichael Cree 
28979f8671SMichael Cree /* The maximum number of PMCs on any Alpha CPU whatsoever. */
29979f8671SMichael Cree #define MAX_HWEVENTS 3
30979f8671SMichael Cree #define PMC_NO_INDEX -1
31979f8671SMichael Cree 
32979f8671SMichael Cree /* For tracking PMCs and the hw events they monitor on each CPU. */
33979f8671SMichael Cree struct cpu_hw_events {
34979f8671SMichael Cree 	int			enabled;
35979f8671SMichael Cree 	/* Number of events scheduled; also number entries valid in arrays below. */
36979f8671SMichael Cree 	int			n_events;
37979f8671SMichael Cree 	/* Number events added since last hw_perf_disable(). */
38979f8671SMichael Cree 	int			n_added;
39979f8671SMichael Cree 	/* Events currently scheduled. */
40979f8671SMichael Cree 	struct perf_event	*event[MAX_HWEVENTS];
41979f8671SMichael Cree 	/* Event type of each scheduled event. */
42979f8671SMichael Cree 	unsigned long		evtype[MAX_HWEVENTS];
43979f8671SMichael Cree 	/* Current index of each scheduled event; if not yet determined
44979f8671SMichael Cree 	 * contains PMC_NO_INDEX.
45979f8671SMichael Cree 	 */
46979f8671SMichael Cree 	int			current_idx[MAX_HWEVENTS];
47979f8671SMichael Cree 	/* The active PMCs' config for easy use with wrperfmon(). */
48979f8671SMichael Cree 	unsigned long		config;
49979f8671SMichael Cree 	/* The active counters' indices for easy use with wrperfmon(). */
50979f8671SMichael Cree 	unsigned long		idx_mask;
51979f8671SMichael Cree };
52979f8671SMichael Cree DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
53979f8671SMichael Cree 
54979f8671SMichael Cree 
55979f8671SMichael Cree 
56979f8671SMichael Cree /*
57979f8671SMichael Cree  * A structure to hold the description of the PMCs available on a particular
58979f8671SMichael Cree  * type of Alpha CPU.
59979f8671SMichael Cree  */
60979f8671SMichael Cree struct alpha_pmu_t {
61979f8671SMichael Cree 	/* Mapping of the perf system hw event types to indigenous event types */
62979f8671SMichael Cree 	const int *event_map;
63979f8671SMichael Cree 	/* The number of entries in the event_map */
64979f8671SMichael Cree 	int  max_events;
65979f8671SMichael Cree 	/* The number of PMCs on this Alpha */
66979f8671SMichael Cree 	int  num_pmcs;
67979f8671SMichael Cree 	/*
68979f8671SMichael Cree 	 * All PMC counters reside in the IBOX register PCTR.  This is the
69979f8671SMichael Cree 	 * LSB of the counter.
70979f8671SMichael Cree 	 */
71979f8671SMichael Cree 	int  pmc_count_shift[MAX_HWEVENTS];
72979f8671SMichael Cree 	/*
73979f8671SMichael Cree 	 * The mask that isolates the PMC bits when the LSB of the counter
74979f8671SMichael Cree 	 * is shifted to bit 0.
75979f8671SMichael Cree 	 */
76979f8671SMichael Cree 	unsigned long pmc_count_mask[MAX_HWEVENTS];
77979f8671SMichael Cree 	/* The maximum period the PMC can count. */
78979f8671SMichael Cree 	unsigned long pmc_max_period[MAX_HWEVENTS];
79979f8671SMichael Cree 	/*
80979f8671SMichael Cree 	 * The maximum value that may be written to the counter due to
81979f8671SMichael Cree 	 * hardware restrictions is pmc_max_period - pmc_left.
82979f8671SMichael Cree 	 */
83979f8671SMichael Cree 	long pmc_left[3];
84979f8671SMichael Cree 	 /* Subroutine for allocation of PMCs.  Enforces constraints. */
85979f8671SMichael Cree 	int (*check_constraints)(struct perf_event **, unsigned long *, int);
866e22f8f2SWill Deacon 	/* Subroutine for checking validity of a raw event for this PMU. */
876e22f8f2SWill Deacon 	int (*raw_event_valid)(u64 config);
88979f8671SMichael Cree };
89979f8671SMichael Cree 
90979f8671SMichael Cree /*
91979f8671SMichael Cree  * The Alpha CPU PMU description currently in operation.  This is set during
92979f8671SMichael Cree  * the boot process to the specific CPU of the machine.
93979f8671SMichael Cree  */
94979f8671SMichael Cree static const struct alpha_pmu_t *alpha_pmu;
95979f8671SMichael Cree 
96979f8671SMichael Cree 
97979f8671SMichael Cree #define HW_OP_UNSUPPORTED -1
98979f8671SMichael Cree 
99979f8671SMichael Cree /*
100979f8671SMichael Cree  * The hardware description of the EV67, EV68, EV69, EV7 and EV79 PMUs
101979f8671SMichael Cree  * follow. Since they are identical we refer to them collectively as the
102979f8671SMichael Cree  * EV67 henceforth.
103979f8671SMichael Cree  */
104979f8671SMichael Cree 
105979f8671SMichael Cree /*
106979f8671SMichael Cree  * EV67 PMC event types
107979f8671SMichael Cree  *
108979f8671SMichael Cree  * There is no one-to-one mapping of the possible hw event types to the
109979f8671SMichael Cree  * actual codes that are used to program the PMCs hence we introduce our
110979f8671SMichael Cree  * own hw event type identifiers.
111979f8671SMichael Cree  */
112979f8671SMichael Cree enum ev67_pmc_event_type {
113979f8671SMichael Cree 	EV67_CYCLES = 1,
114979f8671SMichael Cree 	EV67_INSTRUCTIONS,
115979f8671SMichael Cree 	EV67_BCACHEMISS,
116979f8671SMichael Cree 	EV67_MBOXREPLAY,
117979f8671SMichael Cree 	EV67_LAST_ET
118979f8671SMichael Cree };
119979f8671SMichael Cree #define EV67_NUM_EVENT_TYPES (EV67_LAST_ET-EV67_CYCLES)
120979f8671SMichael Cree 
121979f8671SMichael Cree 
122979f8671SMichael Cree /* Mapping of the hw event types to the perf tool interface */
123979f8671SMichael Cree static const int ev67_perfmon_event_map[] = {
124979f8671SMichael Cree 	[PERF_COUNT_HW_CPU_CYCLES]	 = EV67_CYCLES,
125979f8671SMichael Cree 	[PERF_COUNT_HW_INSTRUCTIONS]	 = EV67_INSTRUCTIONS,
126979f8671SMichael Cree 	[PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
127979f8671SMichael Cree 	[PERF_COUNT_HW_CACHE_MISSES]	 = EV67_BCACHEMISS,
128979f8671SMichael Cree };
129979f8671SMichael Cree 
130979f8671SMichael Cree struct ev67_mapping_t {
131979f8671SMichael Cree 	int config;
132979f8671SMichael Cree 	int idx;
133979f8671SMichael Cree };
134979f8671SMichael Cree 
135979f8671SMichael Cree /*
136979f8671SMichael Cree  * The mapping used for one event only - these must be in same order as enum
137979f8671SMichael Cree  * ev67_pmc_event_type definition.
138979f8671SMichael Cree  */
139979f8671SMichael Cree static const struct ev67_mapping_t ev67_mapping[] = {
140979f8671SMichael Cree 	{EV67_PCTR_INSTR_CYCLES, 1},	 /* EV67_CYCLES, */
141979f8671SMichael Cree 	{EV67_PCTR_INSTR_CYCLES, 0},	 /* EV67_INSTRUCTIONS */
142979f8671SMichael Cree 	{EV67_PCTR_INSTR_BCACHEMISS, 1}, /* EV67_BCACHEMISS */
143979f8671SMichael Cree 	{EV67_PCTR_CYCLES_MBOX, 1}	 /* EV67_MBOXREPLAY */
144979f8671SMichael Cree };
145979f8671SMichael Cree 
146979f8671SMichael Cree 
147979f8671SMichael Cree /*
148979f8671SMichael Cree  * Check that a group of events can be simultaneously scheduled on to the
149979f8671SMichael Cree  * EV67 PMU.  Also allocate counter indices and config.
150979f8671SMichael Cree  */
151979f8671SMichael Cree static int ev67_check_constraints(struct perf_event **event,
152979f8671SMichael Cree 				unsigned long *evtype, int n_ev)
153979f8671SMichael Cree {
154979f8671SMichael Cree 	int idx0;
155979f8671SMichael Cree 	unsigned long config;
156979f8671SMichael Cree 
157979f8671SMichael Cree 	idx0 = ev67_mapping[evtype[0]-1].idx;
158979f8671SMichael Cree 	config = ev67_mapping[evtype[0]-1].config;
159979f8671SMichael Cree 	if (n_ev == 1)
160979f8671SMichael Cree 		goto success;
161979f8671SMichael Cree 
162979f8671SMichael Cree 	BUG_ON(n_ev != 2);
163979f8671SMichael Cree 
164979f8671SMichael Cree 	if (evtype[0] == EV67_MBOXREPLAY || evtype[1] == EV67_MBOXREPLAY) {
165979f8671SMichael Cree 		/* MBOX replay traps must be on PMC 1 */
166979f8671SMichael Cree 		idx0 = (evtype[0] == EV67_MBOXREPLAY) ? 1 : 0;
167979f8671SMichael Cree 		/* Only cycles can accompany MBOX replay traps */
168979f8671SMichael Cree 		if (evtype[idx0] == EV67_CYCLES) {
169979f8671SMichael Cree 			config = EV67_PCTR_CYCLES_MBOX;
170979f8671SMichael Cree 			goto success;
171979f8671SMichael Cree 		}
172979f8671SMichael Cree 	}
173979f8671SMichael Cree 
174979f8671SMichael Cree 	if (evtype[0] == EV67_BCACHEMISS || evtype[1] == EV67_BCACHEMISS) {
175979f8671SMichael Cree 		/* Bcache misses must be on PMC 1 */
176979f8671SMichael Cree 		idx0 = (evtype[0] == EV67_BCACHEMISS) ? 1 : 0;
177979f8671SMichael Cree 		/* Only instructions can accompany Bcache misses */
178979f8671SMichael Cree 		if (evtype[idx0] == EV67_INSTRUCTIONS) {
179979f8671SMichael Cree 			config = EV67_PCTR_INSTR_BCACHEMISS;
180979f8671SMichael Cree 			goto success;
181979f8671SMichael Cree 		}
182979f8671SMichael Cree 	}
183979f8671SMichael Cree 
184979f8671SMichael Cree 	if (evtype[0] == EV67_INSTRUCTIONS || evtype[1] == EV67_INSTRUCTIONS) {
185979f8671SMichael Cree 		/* Instructions must be on PMC 0 */
186979f8671SMichael Cree 		idx0 = (evtype[0] == EV67_INSTRUCTIONS) ? 0 : 1;
187979f8671SMichael Cree 		/* By this point only cycles can accompany instructions */
188979f8671SMichael Cree 		if (evtype[idx0^1] == EV67_CYCLES) {
189979f8671SMichael Cree 			config = EV67_PCTR_INSTR_CYCLES;
190979f8671SMichael Cree 			goto success;
191979f8671SMichael Cree 		}
192979f8671SMichael Cree 	}
193979f8671SMichael Cree 
194979f8671SMichael Cree 	/* Otherwise, darn it, there is a conflict.  */
195979f8671SMichael Cree 	return -1;
196979f8671SMichael Cree 
197979f8671SMichael Cree success:
198979f8671SMichael Cree 	event[0]->hw.idx = idx0;
199979f8671SMichael Cree 	event[0]->hw.config_base = config;
200979f8671SMichael Cree 	if (n_ev == 2) {
201979f8671SMichael Cree 		event[1]->hw.idx = idx0 ^ 1;
202979f8671SMichael Cree 		event[1]->hw.config_base = config;
203979f8671SMichael Cree 	}
204979f8671SMichael Cree 	return 0;
205979f8671SMichael Cree }
206979f8671SMichael Cree 
207979f8671SMichael Cree 
2086e22f8f2SWill Deacon static int ev67_raw_event_valid(u64 config)
2096e22f8f2SWill Deacon {
2106e22f8f2SWill Deacon 	return config >= EV67_CYCLES && config < EV67_LAST_ET;
2116e22f8f2SWill Deacon };
2126e22f8f2SWill Deacon 
2136e22f8f2SWill Deacon 
214979f8671SMichael Cree static const struct alpha_pmu_t ev67_pmu = {
215979f8671SMichael Cree 	.event_map = ev67_perfmon_event_map,
216979f8671SMichael Cree 	.max_events = ARRAY_SIZE(ev67_perfmon_event_map),
217979f8671SMichael Cree 	.num_pmcs = 2,
218979f8671SMichael Cree 	.pmc_count_shift = {EV67_PCTR_0_COUNT_SHIFT, EV67_PCTR_1_COUNT_SHIFT, 0},
219979f8671SMichael Cree 	.pmc_count_mask = {EV67_PCTR_0_COUNT_MASK,  EV67_PCTR_1_COUNT_MASK,  0},
220979f8671SMichael Cree 	.pmc_max_period = {(1UL<<20) - 1, (1UL<<20) - 1, 0},
221979f8671SMichael Cree 	.pmc_left = {16, 4, 0},
2226e22f8f2SWill Deacon 	.check_constraints = ev67_check_constraints,
2236e22f8f2SWill Deacon 	.raw_event_valid = ev67_raw_event_valid,
224979f8671SMichael Cree };
225979f8671SMichael Cree 
226979f8671SMichael Cree 
227979f8671SMichael Cree 
228979f8671SMichael Cree /*
229979f8671SMichael Cree  * Helper routines to ensure that we read/write only the correct PMC bits
230979f8671SMichael Cree  * when calling the wrperfmon PALcall.
231979f8671SMichael Cree  */
232979f8671SMichael Cree static inline void alpha_write_pmc(int idx, unsigned long val)
233979f8671SMichael Cree {
234979f8671SMichael Cree 	val &= alpha_pmu->pmc_count_mask[idx];
235979f8671SMichael Cree 	val <<= alpha_pmu->pmc_count_shift[idx];
236979f8671SMichael Cree 	val |= (1<<idx);
237979f8671SMichael Cree 	wrperfmon(PERFMON_CMD_WRITE, val);
238979f8671SMichael Cree }
239979f8671SMichael Cree 
240979f8671SMichael Cree static inline unsigned long alpha_read_pmc(int idx)
241979f8671SMichael Cree {
242979f8671SMichael Cree 	unsigned long val;
243979f8671SMichael Cree 
244979f8671SMichael Cree 	val = wrperfmon(PERFMON_CMD_READ, 0);
245979f8671SMichael Cree 	val >>= alpha_pmu->pmc_count_shift[idx];
246979f8671SMichael Cree 	val &= alpha_pmu->pmc_count_mask[idx];
247979f8671SMichael Cree 	return val;
248979f8671SMichael Cree }
249979f8671SMichael Cree 
250979f8671SMichael Cree /* Set a new period to sample over */
251979f8671SMichael Cree static int alpha_perf_event_set_period(struct perf_event *event,
252979f8671SMichael Cree 				struct hw_perf_event *hwc, int idx)
253979f8671SMichael Cree {
2547b598cddSMichael Cree 	long left = local64_read(&hwc->period_left);
255979f8671SMichael Cree 	long period = hwc->sample_period;
256979f8671SMichael Cree 	int ret = 0;
257979f8671SMichael Cree 
258979f8671SMichael Cree 	if (unlikely(left <= -period)) {
259979f8671SMichael Cree 		left = period;
2607b598cddSMichael Cree 		local64_set(&hwc->period_left, left);
261979f8671SMichael Cree 		hwc->last_period = period;
262979f8671SMichael Cree 		ret = 1;
263979f8671SMichael Cree 	}
264979f8671SMichael Cree 
265979f8671SMichael Cree 	if (unlikely(left <= 0)) {
266979f8671SMichael Cree 		left += period;
2677b598cddSMichael Cree 		local64_set(&hwc->period_left, left);
268979f8671SMichael Cree 		hwc->last_period = period;
269979f8671SMichael Cree 		ret = 1;
270979f8671SMichael Cree 	}
271979f8671SMichael Cree 
272979f8671SMichael Cree 	/*
273979f8671SMichael Cree 	 * Hardware restrictions require that the counters must not be
274979f8671SMichael Cree 	 * written with values that are too close to the maximum period.
275979f8671SMichael Cree 	 */
276979f8671SMichael Cree 	if (unlikely(left < alpha_pmu->pmc_left[idx]))
277979f8671SMichael Cree 		left = alpha_pmu->pmc_left[idx];
278979f8671SMichael Cree 
279979f8671SMichael Cree 	if (left > (long)alpha_pmu->pmc_max_period[idx])
280979f8671SMichael Cree 		left = alpha_pmu->pmc_max_period[idx];
281979f8671SMichael Cree 
2827b598cddSMichael Cree 	local64_set(&hwc->prev_count, (unsigned long)(-left));
283979f8671SMichael Cree 
284979f8671SMichael Cree 	alpha_write_pmc(idx, (unsigned long)(-left));
285979f8671SMichael Cree 
286979f8671SMichael Cree 	perf_event_update_userpage(event);
287979f8671SMichael Cree 
288979f8671SMichael Cree 	return ret;
289979f8671SMichael Cree }
290979f8671SMichael Cree 
291979f8671SMichael Cree 
292979f8671SMichael Cree /*
293979f8671SMichael Cree  * Calculates the count (the 'delta') since the last time the PMC was read.
294979f8671SMichael Cree  *
295979f8671SMichael Cree  * As the PMCs' full period can easily be exceeded within the perf system
296979f8671SMichael Cree  * sampling period we cannot use any high order bits as a guard bit in the
297979f8671SMichael Cree  * PMCs to detect overflow as is done by other architectures.  The code here
298979f8671SMichael Cree  * calculates the delta on the basis that there is no overflow when ovf is
299979f8671SMichael Cree  * zero.  The value passed via ovf by the interrupt handler corrects for
300979f8671SMichael Cree  * overflow.
301979f8671SMichael Cree  *
302979f8671SMichael Cree  * This can be racey on rare occasions -- a call to this routine can occur
303979f8671SMichael Cree  * with an overflowed counter just before the PMI service routine is called.
304979f8671SMichael Cree  * The check for delta negative hopefully always rectifies this situation.
305979f8671SMichael Cree  */
306979f8671SMichael Cree static unsigned long alpha_perf_event_update(struct perf_event *event,
307979f8671SMichael Cree 					struct hw_perf_event *hwc, int idx, long ovf)
308979f8671SMichael Cree {
309979f8671SMichael Cree 	long prev_raw_count, new_raw_count;
310979f8671SMichael Cree 	long delta;
311979f8671SMichael Cree 
312979f8671SMichael Cree again:
3137b598cddSMichael Cree 	prev_raw_count = local64_read(&hwc->prev_count);
314979f8671SMichael Cree 	new_raw_count = alpha_read_pmc(idx);
315979f8671SMichael Cree 
3167b598cddSMichael Cree 	if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
317979f8671SMichael Cree 			     new_raw_count) != prev_raw_count)
318979f8671SMichael Cree 		goto again;
319979f8671SMichael Cree 
320979f8671SMichael Cree 	delta = (new_raw_count - (prev_raw_count & alpha_pmu->pmc_count_mask[idx])) + ovf;
321979f8671SMichael Cree 
322979f8671SMichael Cree 	/* It is possible on very rare occasions that the PMC has overflowed
323979f8671SMichael Cree 	 * but the interrupt is yet to come.  Detect and fix this situation.
324979f8671SMichael Cree 	 */
325979f8671SMichael Cree 	if (unlikely(delta < 0)) {
326979f8671SMichael Cree 		delta += alpha_pmu->pmc_max_period[idx] + 1;
327979f8671SMichael Cree 	}
328979f8671SMichael Cree 
3297b598cddSMichael Cree 	local64_add(delta, &event->count);
3307b598cddSMichael Cree 	local64_sub(delta, &hwc->period_left);
331979f8671SMichael Cree 
332979f8671SMichael Cree 	return new_raw_count;
333979f8671SMichael Cree }
334979f8671SMichael Cree 
335979f8671SMichael Cree 
336979f8671SMichael Cree /*
337979f8671SMichael Cree  * Collect all HW events into the array event[].
338979f8671SMichael Cree  */
339979f8671SMichael Cree static int collect_events(struct perf_event *group, int max_count,
340979f8671SMichael Cree 			  struct perf_event *event[], unsigned long *evtype,
341979f8671SMichael Cree 			  int *current_idx)
342979f8671SMichael Cree {
343979f8671SMichael Cree 	struct perf_event *pe;
344979f8671SMichael Cree 	int n = 0;
345979f8671SMichael Cree 
346979f8671SMichael Cree 	if (!is_software_event(group)) {
347979f8671SMichael Cree 		if (n >= max_count)
348979f8671SMichael Cree 			return -1;
349979f8671SMichael Cree 		event[n] = group;
350979f8671SMichael Cree 		evtype[n] = group->hw.event_base;
351979f8671SMichael Cree 		current_idx[n++] = PMC_NO_INDEX;
352979f8671SMichael Cree 	}
353979f8671SMichael Cree 	list_for_each_entry(pe, &group->sibling_list, group_entry) {
354979f8671SMichael Cree 		if (!is_software_event(pe) && pe->state != PERF_EVENT_STATE_OFF) {
355979f8671SMichael Cree 			if (n >= max_count)
356979f8671SMichael Cree 				return -1;
357979f8671SMichael Cree 			event[n] = pe;
358979f8671SMichael Cree 			evtype[n] = pe->hw.event_base;
359979f8671SMichael Cree 			current_idx[n++] = PMC_NO_INDEX;
360979f8671SMichael Cree 		}
361979f8671SMichael Cree 	}
362979f8671SMichael Cree 	return n;
363979f8671SMichael Cree }
364979f8671SMichael Cree 
365979f8671SMichael Cree 
366979f8671SMichael Cree 
367979f8671SMichael Cree /*
368979f8671SMichael Cree  * Check that a group of events can be simultaneously scheduled on to the PMU.
369979f8671SMichael Cree  */
370979f8671SMichael Cree static int alpha_check_constraints(struct perf_event **events,
371979f8671SMichael Cree 				   unsigned long *evtypes, int n_ev)
372979f8671SMichael Cree {
373979f8671SMichael Cree 
374979f8671SMichael Cree 	/* No HW events is possible from hw_perf_group_sched_in(). */
375979f8671SMichael Cree 	if (n_ev == 0)
376979f8671SMichael Cree 		return 0;
377979f8671SMichael Cree 
378979f8671SMichael Cree 	if (n_ev > alpha_pmu->num_pmcs)
379979f8671SMichael Cree 		return -1;
380979f8671SMichael Cree 
381979f8671SMichael Cree 	return alpha_pmu->check_constraints(events, evtypes, n_ev);
382979f8671SMichael Cree }
383979f8671SMichael Cree 
384979f8671SMichael Cree 
385979f8671SMichael Cree /*
386979f8671SMichael Cree  * If new events have been scheduled then update cpuc with the new
387979f8671SMichael Cree  * configuration.  This may involve shifting cycle counts from one PMC to
388979f8671SMichael Cree  * another.
389979f8671SMichael Cree  */
390979f8671SMichael Cree static void maybe_change_configuration(struct cpu_hw_events *cpuc)
391979f8671SMichael Cree {
392979f8671SMichael Cree 	int j;
393979f8671SMichael Cree 
394979f8671SMichael Cree 	if (cpuc->n_added == 0)
395979f8671SMichael Cree 		return;
396979f8671SMichael Cree 
397979f8671SMichael Cree 	/* Find counters that are moving to another PMC and update */
398979f8671SMichael Cree 	for (j = 0; j < cpuc->n_events; j++) {
399979f8671SMichael Cree 		struct perf_event *pe = cpuc->event[j];
400979f8671SMichael Cree 
401979f8671SMichael Cree 		if (cpuc->current_idx[j] != PMC_NO_INDEX &&
402979f8671SMichael Cree 			cpuc->current_idx[j] != pe->hw.idx) {
403979f8671SMichael Cree 			alpha_perf_event_update(pe, &pe->hw, cpuc->current_idx[j], 0);
404979f8671SMichael Cree 			cpuc->current_idx[j] = PMC_NO_INDEX;
405979f8671SMichael Cree 		}
406979f8671SMichael Cree 	}
407979f8671SMichael Cree 
408979f8671SMichael Cree 	/* Assign to counters all unassigned events. */
409979f8671SMichael Cree 	cpuc->idx_mask = 0;
410979f8671SMichael Cree 	for (j = 0; j < cpuc->n_events; j++) {
411979f8671SMichael Cree 		struct perf_event *pe = cpuc->event[j];
412979f8671SMichael Cree 		struct hw_perf_event *hwc = &pe->hw;
413979f8671SMichael Cree 		int idx = hwc->idx;
414979f8671SMichael Cree 
415a4eaf7f1SPeter Zijlstra 		if (cpuc->current_idx[j] == PMC_NO_INDEX) {
416979f8671SMichael Cree 			alpha_perf_event_set_period(pe, hwc, idx);
417979f8671SMichael Cree 			cpuc->current_idx[j] = idx;
418a4eaf7f1SPeter Zijlstra 		}
419a4eaf7f1SPeter Zijlstra 
420a4eaf7f1SPeter Zijlstra 		if (!(hwc->state & PERF_HES_STOPPED))
421979f8671SMichael Cree 			cpuc->idx_mask |= (1<<cpuc->current_idx[j]);
422979f8671SMichael Cree 	}
423979f8671SMichael Cree 	cpuc->config = cpuc->event[0]->hw.config_base;
424979f8671SMichael Cree }
425979f8671SMichael Cree 
426979f8671SMichael Cree 
427979f8671SMichael Cree 
428979f8671SMichael Cree /* Schedule perf HW event on to PMU.
429979f8671SMichael Cree  *  - this function is called from outside this module via the pmu struct
430979f8671SMichael Cree  *    returned from perf event initialisation.
431979f8671SMichael Cree  */
432a4eaf7f1SPeter Zijlstra static int alpha_pmu_add(struct perf_event *event, int flags)
433979f8671SMichael Cree {
434*2999a4b3SChristoph Lameter 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
43565175c07SMichael Cree 	struct hw_perf_event *hwc = &event->hw;
436979f8671SMichael Cree 	int n0;
437979f8671SMichael Cree 	int ret;
43865175c07SMichael Cree 	unsigned long irq_flags;
439979f8671SMichael Cree 
440979f8671SMichael Cree 	/*
441979f8671SMichael Cree 	 * The Sparc code has the IRQ disable first followed by the perf
442979f8671SMichael Cree 	 * disable, however this can lead to an overflowed counter with the
443979f8671SMichael Cree 	 * PMI disabled on rare occasions.  The alpha_perf_event_update()
444979f8671SMichael Cree 	 * routine should detect this situation by noting a negative delta,
445979f8671SMichael Cree 	 * nevertheless we disable the PMCs first to enable a potential
446979f8671SMichael Cree 	 * final PMI to occur before we disable interrupts.
447979f8671SMichael Cree 	 */
44833696fc0SPeter Zijlstra 	perf_pmu_disable(event->pmu);
44965175c07SMichael Cree 	local_irq_save(irq_flags);
450979f8671SMichael Cree 
451979f8671SMichael Cree 	/* Default to error to be returned */
452979f8671SMichael Cree 	ret = -EAGAIN;
453979f8671SMichael Cree 
454979f8671SMichael Cree 	/* Insert event on to PMU and if successful modify ret to valid return */
455979f8671SMichael Cree 	n0 = cpuc->n_events;
456979f8671SMichael Cree 	if (n0 < alpha_pmu->num_pmcs) {
457979f8671SMichael Cree 		cpuc->event[n0] = event;
458979f8671SMichael Cree 		cpuc->evtype[n0] = event->hw.event_base;
459979f8671SMichael Cree 		cpuc->current_idx[n0] = PMC_NO_INDEX;
460979f8671SMichael Cree 
461979f8671SMichael Cree 		if (!alpha_check_constraints(cpuc->event, cpuc->evtype, n0+1)) {
462979f8671SMichael Cree 			cpuc->n_events++;
463979f8671SMichael Cree 			cpuc->n_added++;
464979f8671SMichael Cree 			ret = 0;
465979f8671SMichael Cree 		}
466979f8671SMichael Cree 	}
467979f8671SMichael Cree 
468a4eaf7f1SPeter Zijlstra 	hwc->state = PERF_HES_UPTODATE;
469a4eaf7f1SPeter Zijlstra 	if (!(flags & PERF_EF_START))
470a4eaf7f1SPeter Zijlstra 		hwc->state |= PERF_HES_STOPPED;
471a4eaf7f1SPeter Zijlstra 
47265175c07SMichael Cree 	local_irq_restore(irq_flags);
47333696fc0SPeter Zijlstra 	perf_pmu_enable(event->pmu);
474979f8671SMichael Cree 
475979f8671SMichael Cree 	return ret;
476979f8671SMichael Cree }
477979f8671SMichael Cree 
478979f8671SMichael Cree 
479979f8671SMichael Cree 
480979f8671SMichael Cree /* Disable performance monitoring unit
481979f8671SMichael Cree  *  - this function is called from outside this module via the pmu struct
482979f8671SMichael Cree  *    returned from perf event initialisation.
483979f8671SMichael Cree  */
484a4eaf7f1SPeter Zijlstra static void alpha_pmu_del(struct perf_event *event, int flags)
485979f8671SMichael Cree {
486*2999a4b3SChristoph Lameter 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
487979f8671SMichael Cree 	struct hw_perf_event *hwc = &event->hw;
48865175c07SMichael Cree 	unsigned long irq_flags;
489979f8671SMichael Cree 	int j;
490979f8671SMichael Cree 
49133696fc0SPeter Zijlstra 	perf_pmu_disable(event->pmu);
49265175c07SMichael Cree 	local_irq_save(irq_flags);
493979f8671SMichael Cree 
494979f8671SMichael Cree 	for (j = 0; j < cpuc->n_events; j++) {
495979f8671SMichael Cree 		if (event == cpuc->event[j]) {
496979f8671SMichael Cree 			int idx = cpuc->current_idx[j];
497979f8671SMichael Cree 
498979f8671SMichael Cree 			/* Shift remaining entries down into the existing
499979f8671SMichael Cree 			 * slot.
500979f8671SMichael Cree 			 */
501979f8671SMichael Cree 			while (++j < cpuc->n_events) {
502979f8671SMichael Cree 				cpuc->event[j - 1] = cpuc->event[j];
503979f8671SMichael Cree 				cpuc->evtype[j - 1] = cpuc->evtype[j];
504979f8671SMichael Cree 				cpuc->current_idx[j - 1] =
505979f8671SMichael Cree 					cpuc->current_idx[j];
506979f8671SMichael Cree 			}
507979f8671SMichael Cree 
508979f8671SMichael Cree 			/* Absorb the final count and turn off the event. */
509979f8671SMichael Cree 			alpha_perf_event_update(event, hwc, idx, 0);
510979f8671SMichael Cree 			perf_event_update_userpage(event);
511979f8671SMichael Cree 
512979f8671SMichael Cree 			cpuc->idx_mask &= ~(1UL<<idx);
513979f8671SMichael Cree 			cpuc->n_events--;
514979f8671SMichael Cree 			break;
515979f8671SMichael Cree 		}
516979f8671SMichael Cree 	}
517979f8671SMichael Cree 
51865175c07SMichael Cree 	local_irq_restore(irq_flags);
51933696fc0SPeter Zijlstra 	perf_pmu_enable(event->pmu);
520979f8671SMichael Cree }
521979f8671SMichael Cree 
522979f8671SMichael Cree 
523979f8671SMichael Cree static void alpha_pmu_read(struct perf_event *event)
524979f8671SMichael Cree {
525979f8671SMichael Cree 	struct hw_perf_event *hwc = &event->hw;
526979f8671SMichael Cree 
527979f8671SMichael Cree 	alpha_perf_event_update(event, hwc, hwc->idx, 0);
528979f8671SMichael Cree }
529979f8671SMichael Cree 
530979f8671SMichael Cree 
531a4eaf7f1SPeter Zijlstra static void alpha_pmu_stop(struct perf_event *event, int flags)
532979f8671SMichael Cree {
533979f8671SMichael Cree 	struct hw_perf_event *hwc = &event->hw;
534*2999a4b3SChristoph Lameter 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
535979f8671SMichael Cree 
536a4eaf7f1SPeter Zijlstra 	if (!(hwc->state & PERF_HES_STOPPED)) {
53765175c07SMichael Cree 		cpuc->idx_mask &= ~(1UL<<hwc->idx);
538a4eaf7f1SPeter Zijlstra 		hwc->state |= PERF_HES_STOPPED;
539a4eaf7f1SPeter Zijlstra 	}
540a4eaf7f1SPeter Zijlstra 
541a4eaf7f1SPeter Zijlstra 	if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
542a4eaf7f1SPeter Zijlstra 		alpha_perf_event_update(event, hwc, hwc->idx, 0);
543a4eaf7f1SPeter Zijlstra 		hwc->state |= PERF_HES_UPTODATE;
544a4eaf7f1SPeter Zijlstra 	}
545a4eaf7f1SPeter Zijlstra 
546a4eaf7f1SPeter Zijlstra 	if (cpuc->enabled)
54765175c07SMichael Cree 		wrperfmon(PERFMON_CMD_DISABLE, (1UL<<hwc->idx));
548a4eaf7f1SPeter Zijlstra }
549a4eaf7f1SPeter Zijlstra 
550a4eaf7f1SPeter Zijlstra 
551a4eaf7f1SPeter Zijlstra static void alpha_pmu_start(struct perf_event *event, int flags)
552a4eaf7f1SPeter Zijlstra {
553a4eaf7f1SPeter Zijlstra 	struct hw_perf_event *hwc = &event->hw;
554*2999a4b3SChristoph Lameter 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
555a4eaf7f1SPeter Zijlstra 
556a4eaf7f1SPeter Zijlstra 	if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
557a4eaf7f1SPeter Zijlstra 		return;
558a4eaf7f1SPeter Zijlstra 
559a4eaf7f1SPeter Zijlstra 	if (flags & PERF_EF_RELOAD) {
560a4eaf7f1SPeter Zijlstra 		WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
561a4eaf7f1SPeter Zijlstra 		alpha_perf_event_set_period(event, hwc, hwc->idx);
562a4eaf7f1SPeter Zijlstra 	}
563a4eaf7f1SPeter Zijlstra 
564a4eaf7f1SPeter Zijlstra 	hwc->state = 0;
565a4eaf7f1SPeter Zijlstra 
566979f8671SMichael Cree 	cpuc->idx_mask |= 1UL<<hwc->idx;
567a4eaf7f1SPeter Zijlstra 	if (cpuc->enabled)
568979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));
569979f8671SMichael Cree }
570979f8671SMichael Cree 
571979f8671SMichael Cree 
572979f8671SMichael Cree /*
573979f8671SMichael Cree  * Check that CPU performance counters are supported.
574979f8671SMichael Cree  * - currently support EV67 and later CPUs.
575979f8671SMichael Cree  * - actually some later revisions of the EV6 have the same PMC model as the
576979f8671SMichael Cree  *     EV67 but we don't do suffiently deep CPU detection to detect them.
577979f8671SMichael Cree  *     Bad luck to the very few people who might have one, I guess.
578979f8671SMichael Cree  */
579979f8671SMichael Cree static int supported_cpu(void)
580979f8671SMichael Cree {
581979f8671SMichael Cree 	struct percpu_struct *cpu;
582979f8671SMichael Cree 	unsigned long cputype;
583979f8671SMichael Cree 
584979f8671SMichael Cree 	/* Get cpu type from HW */
585979f8671SMichael Cree 	cpu = (struct percpu_struct *)((char *)hwrpb + hwrpb->processor_offset);
586979f8671SMichael Cree 	cputype = cpu->type & 0xffffffff;
587979f8671SMichael Cree 	/* Include all of EV67, EV68, EV7, EV79 and EV69 as supported. */
588979f8671SMichael Cree 	return (cputype >= EV67_CPU) && (cputype <= EV69_CPU);
589979f8671SMichael Cree }
590979f8671SMichael Cree 
591979f8671SMichael Cree 
592979f8671SMichael Cree 
593979f8671SMichael Cree static void hw_perf_event_destroy(struct perf_event *event)
594979f8671SMichael Cree {
595979f8671SMichael Cree 	/* Nothing to be done! */
596979f8671SMichael Cree 	return;
597979f8671SMichael Cree }
598979f8671SMichael Cree 
599979f8671SMichael Cree 
600979f8671SMichael Cree 
601979f8671SMichael Cree static int __hw_perf_event_init(struct perf_event *event)
602979f8671SMichael Cree {
603979f8671SMichael Cree 	struct perf_event_attr *attr = &event->attr;
604979f8671SMichael Cree 	struct hw_perf_event *hwc = &event->hw;
605979f8671SMichael Cree 	struct perf_event *evts[MAX_HWEVENTS];
606979f8671SMichael Cree 	unsigned long evtypes[MAX_HWEVENTS];
607979f8671SMichael Cree 	int idx_rubbish_bin[MAX_HWEVENTS];
608979f8671SMichael Cree 	int ev;
609979f8671SMichael Cree 	int n;
610979f8671SMichael Cree 
611979f8671SMichael Cree 	/* We only support a limited range of HARDWARE event types with one
612979f8671SMichael Cree 	 * only programmable via a RAW event type.
613979f8671SMichael Cree 	 */
614979f8671SMichael Cree 	if (attr->type == PERF_TYPE_HARDWARE) {
615979f8671SMichael Cree 		if (attr->config >= alpha_pmu->max_events)
616979f8671SMichael Cree 			return -EINVAL;
617979f8671SMichael Cree 		ev = alpha_pmu->event_map[attr->config];
618979f8671SMichael Cree 	} else if (attr->type == PERF_TYPE_HW_CACHE) {
619979f8671SMichael Cree 		return -EOPNOTSUPP;
620979f8671SMichael Cree 	} else if (attr->type == PERF_TYPE_RAW) {
6216e22f8f2SWill Deacon 		if (!alpha_pmu->raw_event_valid(attr->config))
6226e22f8f2SWill Deacon 			return -EINVAL;
6236e22f8f2SWill Deacon 		ev = attr->config;
624979f8671SMichael Cree 	} else {
625979f8671SMichael Cree 		return -EOPNOTSUPP;
626979f8671SMichael Cree 	}
627979f8671SMichael Cree 
628979f8671SMichael Cree 	if (ev < 0) {
629979f8671SMichael Cree 		return ev;
630979f8671SMichael Cree 	}
631979f8671SMichael Cree 
632979f8671SMichael Cree 	/* The EV67 does not support mode exclusion */
633979f8671SMichael Cree 	if (attr->exclude_kernel || attr->exclude_user
634979f8671SMichael Cree 			|| attr->exclude_hv || attr->exclude_idle) {
635979f8671SMichael Cree 		return -EPERM;
636979f8671SMichael Cree 	}
637979f8671SMichael Cree 
638979f8671SMichael Cree 	/*
639979f8671SMichael Cree 	 * We place the event type in event_base here and leave calculation
640979f8671SMichael Cree 	 * of the codes to programme the PMU for alpha_pmu_enable() because
641979f8671SMichael Cree 	 * it is only then we will know what HW events are actually
642979f8671SMichael Cree 	 * scheduled on to the PMU.  At that point the code to programme the
643979f8671SMichael Cree 	 * PMU is put into config_base and the PMC to use is placed into
644979f8671SMichael Cree 	 * idx.  We initialise idx (below) to PMC_NO_INDEX to indicate that
645979f8671SMichael Cree 	 * it is yet to be determined.
646979f8671SMichael Cree 	 */
647979f8671SMichael Cree 	hwc->event_base = ev;
648979f8671SMichael Cree 
649979f8671SMichael Cree 	/* Collect events in a group together suitable for calling
650979f8671SMichael Cree 	 * alpha_check_constraints() to verify that the group as a whole can
651979f8671SMichael Cree 	 * be scheduled on to the PMU.
652979f8671SMichael Cree 	 */
653979f8671SMichael Cree 	n = 0;
654979f8671SMichael Cree 	if (event->group_leader != event) {
655979f8671SMichael Cree 		n = collect_events(event->group_leader,
656979f8671SMichael Cree 				alpha_pmu->num_pmcs - 1,
657979f8671SMichael Cree 				evts, evtypes, idx_rubbish_bin);
658979f8671SMichael Cree 		if (n < 0)
659979f8671SMichael Cree 			return -EINVAL;
660979f8671SMichael Cree 	}
661979f8671SMichael Cree 	evtypes[n] = hwc->event_base;
662979f8671SMichael Cree 	evts[n] = event;
663979f8671SMichael Cree 
664979f8671SMichael Cree 	if (alpha_check_constraints(evts, evtypes, n + 1))
665979f8671SMichael Cree 		return -EINVAL;
666979f8671SMichael Cree 
667979f8671SMichael Cree 	/* Indicate that PMU config and idx are yet to be determined. */
668979f8671SMichael Cree 	hwc->config_base = 0;
669979f8671SMichael Cree 	hwc->idx = PMC_NO_INDEX;
670979f8671SMichael Cree 
671979f8671SMichael Cree 	event->destroy = hw_perf_event_destroy;
672979f8671SMichael Cree 
673979f8671SMichael Cree 	/*
674979f8671SMichael Cree 	 * Most architectures reserve the PMU for their use at this point.
675979f8671SMichael Cree 	 * As there is no existing mechanism to arbitrate usage and there
676979f8671SMichael Cree 	 * appears to be no other user of the Alpha PMU we just assume
677979f8671SMichael Cree 	 * that we can just use it, hence a NO-OP here.
678979f8671SMichael Cree 	 *
679979f8671SMichael Cree 	 * Maybe an alpha_reserve_pmu() routine should be implemented but is
680979f8671SMichael Cree 	 * anything else ever going to use it?
681979f8671SMichael Cree 	 */
682979f8671SMichael Cree 
683979f8671SMichael Cree 	if (!hwc->sample_period) {
684979f8671SMichael Cree 		hwc->sample_period = alpha_pmu->pmc_max_period[0];
685979f8671SMichael Cree 		hwc->last_period = hwc->sample_period;
6867b598cddSMichael Cree 		local64_set(&hwc->period_left, hwc->sample_period);
687979f8671SMichael Cree 	}
688979f8671SMichael Cree 
689979f8671SMichael Cree 	return 0;
690979f8671SMichael Cree }
691979f8671SMichael Cree 
692b0a873ebSPeter Zijlstra /*
693b0a873ebSPeter Zijlstra  * Main entry point to initialise a HW performance event.
694b0a873ebSPeter Zijlstra  */
695b0a873ebSPeter Zijlstra static int alpha_pmu_event_init(struct perf_event *event)
696b0a873ebSPeter Zijlstra {
697b0a873ebSPeter Zijlstra 	int err;
698b0a873ebSPeter Zijlstra 
6992481c5faSStephane Eranian 	/* does not support taken branch sampling */
7002481c5faSStephane Eranian 	if (has_branch_stack(event))
7012481c5faSStephane Eranian 		return -EOPNOTSUPP;
7022481c5faSStephane Eranian 
703b0a873ebSPeter Zijlstra 	switch (event->attr.type) {
704b0a873ebSPeter Zijlstra 	case PERF_TYPE_RAW:
705b0a873ebSPeter Zijlstra 	case PERF_TYPE_HARDWARE:
706b0a873ebSPeter Zijlstra 	case PERF_TYPE_HW_CACHE:
707b0a873ebSPeter Zijlstra 		break;
708b0a873ebSPeter Zijlstra 
709b0a873ebSPeter Zijlstra 	default:
710b0a873ebSPeter Zijlstra 		return -ENOENT;
711b0a873ebSPeter Zijlstra 	}
712b0a873ebSPeter Zijlstra 
713b0a873ebSPeter Zijlstra 	if (!alpha_pmu)
714b0a873ebSPeter Zijlstra 		return -ENODEV;
715b0a873ebSPeter Zijlstra 
716b0a873ebSPeter Zijlstra 	/* Do the real initialisation work. */
717b0a873ebSPeter Zijlstra 	err = __hw_perf_event_init(event);
718b0a873ebSPeter Zijlstra 
719b0a873ebSPeter Zijlstra 	return err;
720b0a873ebSPeter Zijlstra }
721b0a873ebSPeter Zijlstra 
722979f8671SMichael Cree /*
723979f8671SMichael Cree  * Main entry point - enable HW performance counters.
724979f8671SMichael Cree  */
725a4eaf7f1SPeter Zijlstra static void alpha_pmu_enable(struct pmu *pmu)
726979f8671SMichael Cree {
727*2999a4b3SChristoph Lameter 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
728979f8671SMichael Cree 
729979f8671SMichael Cree 	if (cpuc->enabled)
730979f8671SMichael Cree 		return;
731979f8671SMichael Cree 
732979f8671SMichael Cree 	cpuc->enabled = 1;
733979f8671SMichael Cree 	barrier();
734979f8671SMichael Cree 
735979f8671SMichael Cree 	if (cpuc->n_events > 0) {
736979f8671SMichael Cree 		/* Update cpuc with information from any new scheduled events. */
737979f8671SMichael Cree 		maybe_change_configuration(cpuc);
738979f8671SMichael Cree 
739979f8671SMichael Cree 		/* Start counting the desired events. */
740979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_LOGGING_OPTIONS, EV67_PCTR_MODE_AGGREGATE);
741979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_DESIRED_EVENTS, cpuc->config);
742979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
743979f8671SMichael Cree 	}
744979f8671SMichael Cree }
745979f8671SMichael Cree 
746979f8671SMichael Cree 
747979f8671SMichael Cree /*
748979f8671SMichael Cree  * Main entry point - disable HW performance counters.
749979f8671SMichael Cree  */
750979f8671SMichael Cree 
751a4eaf7f1SPeter Zijlstra static void alpha_pmu_disable(struct pmu *pmu)
752979f8671SMichael Cree {
753*2999a4b3SChristoph Lameter 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
754979f8671SMichael Cree 
755979f8671SMichael Cree 	if (!cpuc->enabled)
756979f8671SMichael Cree 		return;
757979f8671SMichael Cree 
758979f8671SMichael Cree 	cpuc->enabled = 0;
759979f8671SMichael Cree 	cpuc->n_added = 0;
760979f8671SMichael Cree 
761979f8671SMichael Cree 	wrperfmon(PERFMON_CMD_DISABLE, cpuc->idx_mask);
762979f8671SMichael Cree }
763979f8671SMichael Cree 
76433696fc0SPeter Zijlstra static struct pmu pmu = {
765a4eaf7f1SPeter Zijlstra 	.pmu_enable	= alpha_pmu_enable,
766a4eaf7f1SPeter Zijlstra 	.pmu_disable	= alpha_pmu_disable,
76733696fc0SPeter Zijlstra 	.event_init	= alpha_pmu_event_init,
768a4eaf7f1SPeter Zijlstra 	.add		= alpha_pmu_add,
769a4eaf7f1SPeter Zijlstra 	.del		= alpha_pmu_del,
770a4eaf7f1SPeter Zijlstra 	.start		= alpha_pmu_start,
771a4eaf7f1SPeter Zijlstra 	.stop		= alpha_pmu_stop,
77233696fc0SPeter Zijlstra 	.read		= alpha_pmu_read,
77333696fc0SPeter Zijlstra };
77433696fc0SPeter Zijlstra 
775979f8671SMichael Cree 
776979f8671SMichael Cree /*
777979f8671SMichael Cree  * Main entry point - don't know when this is called but it
778979f8671SMichael Cree  * obviously dumps debug info.
779979f8671SMichael Cree  */
780979f8671SMichael Cree void perf_event_print_debug(void)
781979f8671SMichael Cree {
782979f8671SMichael Cree 	unsigned long flags;
783979f8671SMichael Cree 	unsigned long pcr;
784979f8671SMichael Cree 	int pcr0, pcr1;
785979f8671SMichael Cree 	int cpu;
786979f8671SMichael Cree 
787979f8671SMichael Cree 	if (!supported_cpu())
788979f8671SMichael Cree 		return;
789979f8671SMichael Cree 
790979f8671SMichael Cree 	local_irq_save(flags);
791979f8671SMichael Cree 
792979f8671SMichael Cree 	cpu = smp_processor_id();
793979f8671SMichael Cree 
794979f8671SMichael Cree 	pcr = wrperfmon(PERFMON_CMD_READ, 0);
795979f8671SMichael Cree 	pcr0 = (pcr >> alpha_pmu->pmc_count_shift[0]) & alpha_pmu->pmc_count_mask[0];
796979f8671SMichael Cree 	pcr1 = (pcr >> alpha_pmu->pmc_count_shift[1]) & alpha_pmu->pmc_count_mask[1];
797979f8671SMichael Cree 
798979f8671SMichael Cree 	pr_info("CPU#%d: PCTR0[%06x] PCTR1[%06x]\n", cpu, pcr0, pcr1);
799979f8671SMichael Cree 
800979f8671SMichael Cree 	local_irq_restore(flags);
801979f8671SMichael Cree }
802979f8671SMichael Cree 
803979f8671SMichael Cree 
804979f8671SMichael Cree /*
805979f8671SMichael Cree  * Performance Monitoring Interrupt Service Routine called when a PMC
806979f8671SMichael Cree  * overflows.  The PMC that overflowed is passed in la_ptr.
807979f8671SMichael Cree  */
808979f8671SMichael Cree static void alpha_perf_event_irq_handler(unsigned long la_ptr,
809979f8671SMichael Cree 					struct pt_regs *regs)
810979f8671SMichael Cree {
811979f8671SMichael Cree 	struct cpu_hw_events *cpuc;
812979f8671SMichael Cree 	struct perf_sample_data data;
813979f8671SMichael Cree 	struct perf_event *event;
814979f8671SMichael Cree 	struct hw_perf_event *hwc;
815979f8671SMichael Cree 	int idx, j;
816979f8671SMichael Cree 
817*2999a4b3SChristoph Lameter 	__this_cpu_inc(irq_pmi_count);
818*2999a4b3SChristoph Lameter 	cpuc = this_cpu_ptr(&cpu_hw_events);
819979f8671SMichael Cree 
820979f8671SMichael Cree 	/* Completely counting through the PMC's period to trigger a new PMC
821979f8671SMichael Cree 	 * overflow interrupt while in this interrupt routine is utterly
822979f8671SMichael Cree 	 * disastrous!  The EV6 and EV67 counters are sufficiently large to
823979f8671SMichael Cree 	 * prevent this but to be really sure disable the PMCs.
824979f8671SMichael Cree 	 */
825979f8671SMichael Cree 	wrperfmon(PERFMON_CMD_DISABLE, cpuc->idx_mask);
826979f8671SMichael Cree 
827979f8671SMichael Cree 	/* la_ptr is the counter that overflowed. */
82815ac9a39SPeter Zijlstra 	if (unlikely(la_ptr >= alpha_pmu->num_pmcs)) {
829979f8671SMichael Cree 		/* This should never occur! */
830979f8671SMichael Cree 		irq_err_count++;
831979f8671SMichael Cree 		pr_warning("PMI: silly index %ld\n", la_ptr);
832979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
833979f8671SMichael Cree 		return;
834979f8671SMichael Cree 	}
835979f8671SMichael Cree 
836979f8671SMichael Cree 	idx = la_ptr;
837979f8671SMichael Cree 
838979f8671SMichael Cree 	for (j = 0; j < cpuc->n_events; j++) {
839979f8671SMichael Cree 		if (cpuc->current_idx[j] == idx)
840979f8671SMichael Cree 			break;
841979f8671SMichael Cree 	}
842979f8671SMichael Cree 
843979f8671SMichael Cree 	if (unlikely(j == cpuc->n_events)) {
844979f8671SMichael Cree 		/* This can occur if the event is disabled right on a PMC overflow. */
845979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
846979f8671SMichael Cree 		return;
847979f8671SMichael Cree 	}
848979f8671SMichael Cree 
849979f8671SMichael Cree 	event = cpuc->event[j];
850979f8671SMichael Cree 
851979f8671SMichael Cree 	if (unlikely(!event)) {
852979f8671SMichael Cree 		/* This should never occur! */
853979f8671SMichael Cree 		irq_err_count++;
854979f8671SMichael Cree 		pr_warning("PMI: No event at index %d!\n", idx);
855979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
856979f8671SMichael Cree 		return;
857979f8671SMichael Cree 	}
858979f8671SMichael Cree 
859979f8671SMichael Cree 	hwc = &event->hw;
860979f8671SMichael Cree 	alpha_perf_event_update(event, hwc, idx, alpha_pmu->pmc_max_period[idx]+1);
861fd0d000bSRobert Richter 	perf_sample_data_init(&data, 0, hwc->last_period);
862979f8671SMichael Cree 
863979f8671SMichael Cree 	if (alpha_perf_event_set_period(event, hwc, idx)) {
864a8b0ca17SPeter Zijlstra 		if (perf_event_overflow(event, &data, regs)) {
865979f8671SMichael Cree 			/* Interrupts coming too quickly; "throttle" the
866979f8671SMichael Cree 			 * counter, i.e., disable it for a little while.
867979f8671SMichael Cree 			 */
86865175c07SMichael Cree 			alpha_pmu_stop(event, 0);
869979f8671SMichael Cree 		}
870979f8671SMichael Cree 	}
871979f8671SMichael Cree 	wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
872979f8671SMichael Cree 
873979f8671SMichael Cree 	return;
874979f8671SMichael Cree }
875979f8671SMichael Cree 
876979f8671SMichael Cree 
877979f8671SMichael Cree 
878979f8671SMichael Cree /*
879979f8671SMichael Cree  * Init call to initialise performance events at kernel startup.
880979f8671SMichael Cree  */
881004417a6SPeter Zijlstra int __init init_hw_perf_events(void)
882979f8671SMichael Cree {
883979f8671SMichael Cree 	pr_info("Performance events: ");
884979f8671SMichael Cree 
885979f8671SMichael Cree 	if (!supported_cpu()) {
886979f8671SMichael Cree 		pr_cont("No support for your CPU.\n");
887004417a6SPeter Zijlstra 		return 0;
888979f8671SMichael Cree 	}
889979f8671SMichael Cree 
890979f8671SMichael Cree 	pr_cont("Supported CPU type!\n");
891979f8671SMichael Cree 
892979f8671SMichael Cree 	/* Override performance counter IRQ vector */
893979f8671SMichael Cree 
894979f8671SMichael Cree 	perf_irq = alpha_perf_event_irq_handler;
895979f8671SMichael Cree 
896979f8671SMichael Cree 	/* And set up PMU specification */
897979f8671SMichael Cree 	alpha_pmu = &ev67_pmu;
898b0a873ebSPeter Zijlstra 
8992e80a82aSPeter Zijlstra 	perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
900979f8671SMichael Cree 
901004417a6SPeter Zijlstra 	return 0;
902004417a6SPeter Zijlstra }
903004417a6SPeter Zijlstra early_initcall(init_hw_perf_events);
904