xref: /openbmc/linux/arch/alpha/kernel/perf_event.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2979f8671SMichael Cree /*
3979f8671SMichael Cree  * Hardware performance events for the Alpha.
4979f8671SMichael Cree  *
5979f8671SMichael Cree  * We implement HW counts on the EV67 and subsequent CPUs only.
6979f8671SMichael Cree  *
7979f8671SMichael Cree  * (C) 2010 Michael J. Cree
8979f8671SMichael Cree  *
9979f8671SMichael Cree  * Somewhat based on the Sparc code, and to a lesser extent the PowerPC and
10979f8671SMichael Cree  * ARM code, which are copyright by their respective authors.
11979f8671SMichael Cree  */
12979f8671SMichael Cree 
13979f8671SMichael Cree #include <linux/perf_event.h>
14979f8671SMichael Cree #include <linux/kprobes.h>
15979f8671SMichael Cree #include <linux/kernel.h>
16979f8671SMichael Cree #include <linux/kdebug.h>
17979f8671SMichael Cree #include <linux/mutex.h>
18004417a6SPeter Zijlstra #include <linux/init.h>
19979f8671SMichael Cree 
20979f8671SMichael Cree #include <asm/hwrpb.h>
2160063497SArun Sharma #include <linux/atomic.h>
22979f8671SMichael Cree #include <asm/irq.h>
23979f8671SMichael Cree #include <asm/irq_regs.h>
24979f8671SMichael Cree #include <asm/pal.h>
25979f8671SMichael Cree #include <asm/wrperfmon.h>
26979f8671SMichael Cree #include <asm/hw_irq.h>
27979f8671SMichael Cree 
28979f8671SMichael Cree 
29979f8671SMichael Cree /* The maximum number of PMCs on any Alpha CPU whatsoever. */
30979f8671SMichael Cree #define MAX_HWEVENTS 3
31979f8671SMichael Cree #define PMC_NO_INDEX -1
32979f8671SMichael Cree 
33979f8671SMichael Cree /* For tracking PMCs and the hw events they monitor on each CPU. */
34979f8671SMichael Cree struct cpu_hw_events {
35979f8671SMichael Cree 	int			enabled;
36979f8671SMichael Cree 	/* Number of events scheduled; also number entries valid in arrays below. */
37979f8671SMichael Cree 	int			n_events;
38979f8671SMichael Cree 	/* Number events added since last hw_perf_disable(). */
39979f8671SMichael Cree 	int			n_added;
40979f8671SMichael Cree 	/* Events currently scheduled. */
41979f8671SMichael Cree 	struct perf_event	*event[MAX_HWEVENTS];
42979f8671SMichael Cree 	/* Event type of each scheduled event. */
43979f8671SMichael Cree 	unsigned long		evtype[MAX_HWEVENTS];
44979f8671SMichael Cree 	/* Current index of each scheduled event; if not yet determined
45979f8671SMichael Cree 	 * contains PMC_NO_INDEX.
46979f8671SMichael Cree 	 */
47979f8671SMichael Cree 	int			current_idx[MAX_HWEVENTS];
48979f8671SMichael Cree 	/* The active PMCs' config for easy use with wrperfmon(). */
49979f8671SMichael Cree 	unsigned long		config;
50979f8671SMichael Cree 	/* The active counters' indices for easy use with wrperfmon(). */
51979f8671SMichael Cree 	unsigned long		idx_mask;
52979f8671SMichael Cree };
53979f8671SMichael Cree DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
54979f8671SMichael Cree 
55979f8671SMichael Cree 
56979f8671SMichael Cree 
57979f8671SMichael Cree /*
58979f8671SMichael Cree  * A structure to hold the description of the PMCs available on a particular
59979f8671SMichael Cree  * type of Alpha CPU.
60979f8671SMichael Cree  */
61979f8671SMichael Cree struct alpha_pmu_t {
62979f8671SMichael Cree 	/* Mapping of the perf system hw event types to indigenous event types */
63979f8671SMichael Cree 	const int *event_map;
64979f8671SMichael Cree 	/* The number of entries in the event_map */
65979f8671SMichael Cree 	int  max_events;
66979f8671SMichael Cree 	/* The number of PMCs on this Alpha */
67979f8671SMichael Cree 	int  num_pmcs;
68979f8671SMichael Cree 	/*
69979f8671SMichael Cree 	 * All PMC counters reside in the IBOX register PCTR.  This is the
70979f8671SMichael Cree 	 * LSB of the counter.
71979f8671SMichael Cree 	 */
72979f8671SMichael Cree 	int  pmc_count_shift[MAX_HWEVENTS];
73979f8671SMichael Cree 	/*
74979f8671SMichael Cree 	 * The mask that isolates the PMC bits when the LSB of the counter
75979f8671SMichael Cree 	 * is shifted to bit 0.
76979f8671SMichael Cree 	 */
77979f8671SMichael Cree 	unsigned long pmc_count_mask[MAX_HWEVENTS];
78979f8671SMichael Cree 	/* The maximum period the PMC can count. */
79979f8671SMichael Cree 	unsigned long pmc_max_period[MAX_HWEVENTS];
80979f8671SMichael Cree 	/*
81979f8671SMichael Cree 	 * The maximum value that may be written to the counter due to
82979f8671SMichael Cree 	 * hardware restrictions is pmc_max_period - pmc_left.
83979f8671SMichael Cree 	 */
84979f8671SMichael Cree 	long pmc_left[3];
85979f8671SMichael Cree 	 /* Subroutine for allocation of PMCs.  Enforces constraints. */
86979f8671SMichael Cree 	int (*check_constraints)(struct perf_event **, unsigned long *, int);
876e22f8f2SWill Deacon 	/* Subroutine for checking validity of a raw event for this PMU. */
886e22f8f2SWill Deacon 	int (*raw_event_valid)(u64 config);
89979f8671SMichael Cree };
90979f8671SMichael Cree 
91979f8671SMichael Cree /*
92979f8671SMichael Cree  * The Alpha CPU PMU description currently in operation.  This is set during
93979f8671SMichael Cree  * the boot process to the specific CPU of the machine.
94979f8671SMichael Cree  */
95979f8671SMichael Cree static const struct alpha_pmu_t *alpha_pmu;
96979f8671SMichael Cree 
97979f8671SMichael Cree 
98979f8671SMichael Cree #define HW_OP_UNSUPPORTED -1
99979f8671SMichael Cree 
100979f8671SMichael Cree /*
101979f8671SMichael Cree  * The hardware description of the EV67, EV68, EV69, EV7 and EV79 PMUs
102979f8671SMichael Cree  * follow. Since they are identical we refer to them collectively as the
103979f8671SMichael Cree  * EV67 henceforth.
104979f8671SMichael Cree  */
105979f8671SMichael Cree 
106979f8671SMichael Cree /*
107979f8671SMichael Cree  * EV67 PMC event types
108979f8671SMichael Cree  *
109979f8671SMichael Cree  * There is no one-to-one mapping of the possible hw event types to the
110979f8671SMichael Cree  * actual codes that are used to program the PMCs hence we introduce our
111979f8671SMichael Cree  * own hw event type identifiers.
112979f8671SMichael Cree  */
113979f8671SMichael Cree enum ev67_pmc_event_type {
114979f8671SMichael Cree 	EV67_CYCLES = 1,
115979f8671SMichael Cree 	EV67_INSTRUCTIONS,
116979f8671SMichael Cree 	EV67_BCACHEMISS,
117979f8671SMichael Cree 	EV67_MBOXREPLAY,
118979f8671SMichael Cree 	EV67_LAST_ET
119979f8671SMichael Cree };
120979f8671SMichael Cree #define EV67_NUM_EVENT_TYPES (EV67_LAST_ET-EV67_CYCLES)
121979f8671SMichael Cree 
122979f8671SMichael Cree 
123979f8671SMichael Cree /* Mapping of the hw event types to the perf tool interface */
124979f8671SMichael Cree static const int ev67_perfmon_event_map[] = {
125979f8671SMichael Cree 	[PERF_COUNT_HW_CPU_CYCLES]	 = EV67_CYCLES,
126979f8671SMichael Cree 	[PERF_COUNT_HW_INSTRUCTIONS]	 = EV67_INSTRUCTIONS,
127979f8671SMichael Cree 	[PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
128979f8671SMichael Cree 	[PERF_COUNT_HW_CACHE_MISSES]	 = EV67_BCACHEMISS,
129979f8671SMichael Cree };
130979f8671SMichael Cree 
131979f8671SMichael Cree struct ev67_mapping_t {
132979f8671SMichael Cree 	int config;
133979f8671SMichael Cree 	int idx;
134979f8671SMichael Cree };
135979f8671SMichael Cree 
136979f8671SMichael Cree /*
137979f8671SMichael Cree  * The mapping used for one event only - these must be in same order as enum
138979f8671SMichael Cree  * ev67_pmc_event_type definition.
139979f8671SMichael Cree  */
140979f8671SMichael Cree static const struct ev67_mapping_t ev67_mapping[] = {
141979f8671SMichael Cree 	{EV67_PCTR_INSTR_CYCLES, 1},	 /* EV67_CYCLES, */
142979f8671SMichael Cree 	{EV67_PCTR_INSTR_CYCLES, 0},	 /* EV67_INSTRUCTIONS */
143979f8671SMichael Cree 	{EV67_PCTR_INSTR_BCACHEMISS, 1}, /* EV67_BCACHEMISS */
144979f8671SMichael Cree 	{EV67_PCTR_CYCLES_MBOX, 1}	 /* EV67_MBOXREPLAY */
145979f8671SMichael Cree };
146979f8671SMichael Cree 
147979f8671SMichael Cree 
148979f8671SMichael Cree /*
149979f8671SMichael Cree  * Check that a group of events can be simultaneously scheduled on to the
150979f8671SMichael Cree  * EV67 PMU.  Also allocate counter indices and config.
151979f8671SMichael Cree  */
ev67_check_constraints(struct perf_event ** event,unsigned long * evtype,int n_ev)152979f8671SMichael Cree static int ev67_check_constraints(struct perf_event **event,
153979f8671SMichael Cree 				unsigned long *evtype, int n_ev)
154979f8671SMichael Cree {
155979f8671SMichael Cree 	int idx0;
156979f8671SMichael Cree 	unsigned long config;
157979f8671SMichael Cree 
158979f8671SMichael Cree 	idx0 = ev67_mapping[evtype[0]-1].idx;
159979f8671SMichael Cree 	config = ev67_mapping[evtype[0]-1].config;
160979f8671SMichael Cree 	if (n_ev == 1)
161979f8671SMichael Cree 		goto success;
162979f8671SMichael Cree 
163979f8671SMichael Cree 	BUG_ON(n_ev != 2);
164979f8671SMichael Cree 
165979f8671SMichael Cree 	if (evtype[0] == EV67_MBOXREPLAY || evtype[1] == EV67_MBOXREPLAY) {
166979f8671SMichael Cree 		/* MBOX replay traps must be on PMC 1 */
167979f8671SMichael Cree 		idx0 = (evtype[0] == EV67_MBOXREPLAY) ? 1 : 0;
168979f8671SMichael Cree 		/* Only cycles can accompany MBOX replay traps */
169979f8671SMichael Cree 		if (evtype[idx0] == EV67_CYCLES) {
170979f8671SMichael Cree 			config = EV67_PCTR_CYCLES_MBOX;
171979f8671SMichael Cree 			goto success;
172979f8671SMichael Cree 		}
173979f8671SMichael Cree 	}
174979f8671SMichael Cree 
175979f8671SMichael Cree 	if (evtype[0] == EV67_BCACHEMISS || evtype[1] == EV67_BCACHEMISS) {
176979f8671SMichael Cree 		/* Bcache misses must be on PMC 1 */
177979f8671SMichael Cree 		idx0 = (evtype[0] == EV67_BCACHEMISS) ? 1 : 0;
178979f8671SMichael Cree 		/* Only instructions can accompany Bcache misses */
179979f8671SMichael Cree 		if (evtype[idx0] == EV67_INSTRUCTIONS) {
180979f8671SMichael Cree 			config = EV67_PCTR_INSTR_BCACHEMISS;
181979f8671SMichael Cree 			goto success;
182979f8671SMichael Cree 		}
183979f8671SMichael Cree 	}
184979f8671SMichael Cree 
185979f8671SMichael Cree 	if (evtype[0] == EV67_INSTRUCTIONS || evtype[1] == EV67_INSTRUCTIONS) {
186979f8671SMichael Cree 		/* Instructions must be on PMC 0 */
187979f8671SMichael Cree 		idx0 = (evtype[0] == EV67_INSTRUCTIONS) ? 0 : 1;
188979f8671SMichael Cree 		/* By this point only cycles can accompany instructions */
189979f8671SMichael Cree 		if (evtype[idx0^1] == EV67_CYCLES) {
190979f8671SMichael Cree 			config = EV67_PCTR_INSTR_CYCLES;
191979f8671SMichael Cree 			goto success;
192979f8671SMichael Cree 		}
193979f8671SMichael Cree 	}
194979f8671SMichael Cree 
195979f8671SMichael Cree 	/* Otherwise, darn it, there is a conflict.  */
196979f8671SMichael Cree 	return -1;
197979f8671SMichael Cree 
198979f8671SMichael Cree success:
199979f8671SMichael Cree 	event[0]->hw.idx = idx0;
200979f8671SMichael Cree 	event[0]->hw.config_base = config;
201979f8671SMichael Cree 	if (n_ev == 2) {
202979f8671SMichael Cree 		event[1]->hw.idx = idx0 ^ 1;
203979f8671SMichael Cree 		event[1]->hw.config_base = config;
204979f8671SMichael Cree 	}
205979f8671SMichael Cree 	return 0;
206979f8671SMichael Cree }
207979f8671SMichael Cree 
208979f8671SMichael Cree 
ev67_raw_event_valid(u64 config)2096e22f8f2SWill Deacon static int ev67_raw_event_valid(u64 config)
2106e22f8f2SWill Deacon {
2116e22f8f2SWill Deacon 	return config >= EV67_CYCLES && config < EV67_LAST_ET;
2126e22f8f2SWill Deacon };
2136e22f8f2SWill Deacon 
2146e22f8f2SWill Deacon 
215979f8671SMichael Cree static const struct alpha_pmu_t ev67_pmu = {
216979f8671SMichael Cree 	.event_map = ev67_perfmon_event_map,
217979f8671SMichael Cree 	.max_events = ARRAY_SIZE(ev67_perfmon_event_map),
218979f8671SMichael Cree 	.num_pmcs = 2,
219979f8671SMichael Cree 	.pmc_count_shift = {EV67_PCTR_0_COUNT_SHIFT, EV67_PCTR_1_COUNT_SHIFT, 0},
220979f8671SMichael Cree 	.pmc_count_mask = {EV67_PCTR_0_COUNT_MASK,  EV67_PCTR_1_COUNT_MASK,  0},
221979f8671SMichael Cree 	.pmc_max_period = {(1UL<<20) - 1, (1UL<<20) - 1, 0},
222979f8671SMichael Cree 	.pmc_left = {16, 4, 0},
2236e22f8f2SWill Deacon 	.check_constraints = ev67_check_constraints,
2246e22f8f2SWill Deacon 	.raw_event_valid = ev67_raw_event_valid,
225979f8671SMichael Cree };
226979f8671SMichael Cree 
227979f8671SMichael Cree 
228979f8671SMichael Cree 
229979f8671SMichael Cree /*
230979f8671SMichael Cree  * Helper routines to ensure that we read/write only the correct PMC bits
231979f8671SMichael Cree  * when calling the wrperfmon PALcall.
232979f8671SMichael Cree  */
alpha_write_pmc(int idx,unsigned long val)233979f8671SMichael Cree static inline void alpha_write_pmc(int idx, unsigned long val)
234979f8671SMichael Cree {
235979f8671SMichael Cree 	val &= alpha_pmu->pmc_count_mask[idx];
236979f8671SMichael Cree 	val <<= alpha_pmu->pmc_count_shift[idx];
237979f8671SMichael Cree 	val |= (1<<idx);
238979f8671SMichael Cree 	wrperfmon(PERFMON_CMD_WRITE, val);
239979f8671SMichael Cree }
240979f8671SMichael Cree 
alpha_read_pmc(int idx)241979f8671SMichael Cree static inline unsigned long alpha_read_pmc(int idx)
242979f8671SMichael Cree {
243979f8671SMichael Cree 	unsigned long val;
244979f8671SMichael Cree 
245979f8671SMichael Cree 	val = wrperfmon(PERFMON_CMD_READ, 0);
246979f8671SMichael Cree 	val >>= alpha_pmu->pmc_count_shift[idx];
247979f8671SMichael Cree 	val &= alpha_pmu->pmc_count_mask[idx];
248979f8671SMichael Cree 	return val;
249979f8671SMichael Cree }
250979f8671SMichael Cree 
251979f8671SMichael Cree /* Set a new period to sample over */
alpha_perf_event_set_period(struct perf_event * event,struct hw_perf_event * hwc,int idx)252979f8671SMichael Cree static int alpha_perf_event_set_period(struct perf_event *event,
253979f8671SMichael Cree 				struct hw_perf_event *hwc, int idx)
254979f8671SMichael Cree {
2557b598cddSMichael Cree 	long left = local64_read(&hwc->period_left);
256979f8671SMichael Cree 	long period = hwc->sample_period;
257979f8671SMichael Cree 	int ret = 0;
258979f8671SMichael Cree 
259979f8671SMichael Cree 	if (unlikely(left <= -period)) {
260979f8671SMichael Cree 		left = period;
2617b598cddSMichael Cree 		local64_set(&hwc->period_left, left);
262979f8671SMichael Cree 		hwc->last_period = period;
263979f8671SMichael Cree 		ret = 1;
264979f8671SMichael Cree 	}
265979f8671SMichael Cree 
266979f8671SMichael Cree 	if (unlikely(left <= 0)) {
267979f8671SMichael Cree 		left += period;
2687b598cddSMichael Cree 		local64_set(&hwc->period_left, left);
269979f8671SMichael Cree 		hwc->last_period = period;
270979f8671SMichael Cree 		ret = 1;
271979f8671SMichael Cree 	}
272979f8671SMichael Cree 
273979f8671SMichael Cree 	/*
274979f8671SMichael Cree 	 * Hardware restrictions require that the counters must not be
275979f8671SMichael Cree 	 * written with values that are too close to the maximum period.
276979f8671SMichael Cree 	 */
277979f8671SMichael Cree 	if (unlikely(left < alpha_pmu->pmc_left[idx]))
278979f8671SMichael Cree 		left = alpha_pmu->pmc_left[idx];
279979f8671SMichael Cree 
280979f8671SMichael Cree 	if (left > (long)alpha_pmu->pmc_max_period[idx])
281979f8671SMichael Cree 		left = alpha_pmu->pmc_max_period[idx];
282979f8671SMichael Cree 
2837b598cddSMichael Cree 	local64_set(&hwc->prev_count, (unsigned long)(-left));
284979f8671SMichael Cree 
285979f8671SMichael Cree 	alpha_write_pmc(idx, (unsigned long)(-left));
286979f8671SMichael Cree 
287979f8671SMichael Cree 	perf_event_update_userpage(event);
288979f8671SMichael Cree 
289979f8671SMichael Cree 	return ret;
290979f8671SMichael Cree }
291979f8671SMichael Cree 
292979f8671SMichael Cree 
293979f8671SMichael Cree /*
294979f8671SMichael Cree  * Calculates the count (the 'delta') since the last time the PMC was read.
295979f8671SMichael Cree  *
296979f8671SMichael Cree  * As the PMCs' full period can easily be exceeded within the perf system
297979f8671SMichael Cree  * sampling period we cannot use any high order bits as a guard bit in the
298979f8671SMichael Cree  * PMCs to detect overflow as is done by other architectures.  The code here
299979f8671SMichael Cree  * calculates the delta on the basis that there is no overflow when ovf is
300979f8671SMichael Cree  * zero.  The value passed via ovf by the interrupt handler corrects for
301979f8671SMichael Cree  * overflow.
302979f8671SMichael Cree  *
303979f8671SMichael Cree  * This can be racey on rare occasions -- a call to this routine can occur
304979f8671SMichael Cree  * with an overflowed counter just before the PMI service routine is called.
305979f8671SMichael Cree  * The check for delta negative hopefully always rectifies this situation.
306979f8671SMichael Cree  */
alpha_perf_event_update(struct perf_event * event,struct hw_perf_event * hwc,int idx,long ovf)307979f8671SMichael Cree static unsigned long alpha_perf_event_update(struct perf_event *event,
308979f8671SMichael Cree 					struct hw_perf_event *hwc, int idx, long ovf)
309979f8671SMichael Cree {
310979f8671SMichael Cree 	long prev_raw_count, new_raw_count;
311979f8671SMichael Cree 	long delta;
312979f8671SMichael Cree 
313979f8671SMichael Cree again:
3147b598cddSMichael Cree 	prev_raw_count = local64_read(&hwc->prev_count);
315979f8671SMichael Cree 	new_raw_count = alpha_read_pmc(idx);
316979f8671SMichael Cree 
3177b598cddSMichael Cree 	if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
318979f8671SMichael Cree 			     new_raw_count) != prev_raw_count)
319979f8671SMichael Cree 		goto again;
320979f8671SMichael Cree 
321979f8671SMichael Cree 	delta = (new_raw_count - (prev_raw_count & alpha_pmu->pmc_count_mask[idx])) + ovf;
322979f8671SMichael Cree 
323979f8671SMichael Cree 	/* It is possible on very rare occasions that the PMC has overflowed
324979f8671SMichael Cree 	 * but the interrupt is yet to come.  Detect and fix this situation.
325979f8671SMichael Cree 	 */
326979f8671SMichael Cree 	if (unlikely(delta < 0)) {
327979f8671SMichael Cree 		delta += alpha_pmu->pmc_max_period[idx] + 1;
328979f8671SMichael Cree 	}
329979f8671SMichael Cree 
3307b598cddSMichael Cree 	local64_add(delta, &event->count);
3317b598cddSMichael Cree 	local64_sub(delta, &hwc->period_left);
332979f8671SMichael Cree 
333979f8671SMichael Cree 	return new_raw_count;
334979f8671SMichael Cree }
335979f8671SMichael Cree 
336979f8671SMichael Cree 
337979f8671SMichael Cree /*
338979f8671SMichael Cree  * Collect all HW events into the array event[].
339979f8671SMichael Cree  */
collect_events(struct perf_event * group,int max_count,struct perf_event * event[],unsigned long * evtype,int * current_idx)340979f8671SMichael Cree static int collect_events(struct perf_event *group, int max_count,
341979f8671SMichael Cree 			  struct perf_event *event[], unsigned long *evtype,
342979f8671SMichael Cree 			  int *current_idx)
343979f8671SMichael Cree {
344979f8671SMichael Cree 	struct perf_event *pe;
345979f8671SMichael Cree 	int n = 0;
346979f8671SMichael Cree 
347979f8671SMichael Cree 	if (!is_software_event(group)) {
348979f8671SMichael Cree 		if (n >= max_count)
349979f8671SMichael Cree 			return -1;
350979f8671SMichael Cree 		event[n] = group;
351979f8671SMichael Cree 		evtype[n] = group->hw.event_base;
352979f8671SMichael Cree 		current_idx[n++] = PMC_NO_INDEX;
353979f8671SMichael Cree 	}
354edb39592SPeter Zijlstra 	for_each_sibling_event(pe, group) {
355979f8671SMichael Cree 		if (!is_software_event(pe) && pe->state != PERF_EVENT_STATE_OFF) {
356979f8671SMichael Cree 			if (n >= max_count)
357979f8671SMichael Cree 				return -1;
358979f8671SMichael Cree 			event[n] = pe;
359979f8671SMichael Cree 			evtype[n] = pe->hw.event_base;
360979f8671SMichael Cree 			current_idx[n++] = PMC_NO_INDEX;
361979f8671SMichael Cree 		}
362979f8671SMichael Cree 	}
363979f8671SMichael Cree 	return n;
364979f8671SMichael Cree }
365979f8671SMichael Cree 
366979f8671SMichael Cree 
367979f8671SMichael Cree 
368979f8671SMichael Cree /*
369979f8671SMichael Cree  * Check that a group of events can be simultaneously scheduled on to the PMU.
370979f8671SMichael Cree  */
alpha_check_constraints(struct perf_event ** events,unsigned long * evtypes,int n_ev)371979f8671SMichael Cree static int alpha_check_constraints(struct perf_event **events,
372979f8671SMichael Cree 				   unsigned long *evtypes, int n_ev)
373979f8671SMichael Cree {
374979f8671SMichael Cree 
375979f8671SMichael Cree 	/* No HW events is possible from hw_perf_group_sched_in(). */
376979f8671SMichael Cree 	if (n_ev == 0)
377979f8671SMichael Cree 		return 0;
378979f8671SMichael Cree 
379979f8671SMichael Cree 	if (n_ev > alpha_pmu->num_pmcs)
380979f8671SMichael Cree 		return -1;
381979f8671SMichael Cree 
382979f8671SMichael Cree 	return alpha_pmu->check_constraints(events, evtypes, n_ev);
383979f8671SMichael Cree }
384979f8671SMichael Cree 
385979f8671SMichael Cree 
386979f8671SMichael Cree /*
387979f8671SMichael Cree  * If new events have been scheduled then update cpuc with the new
388979f8671SMichael Cree  * configuration.  This may involve shifting cycle counts from one PMC to
389979f8671SMichael Cree  * another.
390979f8671SMichael Cree  */
maybe_change_configuration(struct cpu_hw_events * cpuc)391979f8671SMichael Cree static void maybe_change_configuration(struct cpu_hw_events *cpuc)
392979f8671SMichael Cree {
393979f8671SMichael Cree 	int j;
394979f8671SMichael Cree 
395979f8671SMichael Cree 	if (cpuc->n_added == 0)
396979f8671SMichael Cree 		return;
397979f8671SMichael Cree 
398979f8671SMichael Cree 	/* Find counters that are moving to another PMC and update */
399979f8671SMichael Cree 	for (j = 0; j < cpuc->n_events; j++) {
400979f8671SMichael Cree 		struct perf_event *pe = cpuc->event[j];
401979f8671SMichael Cree 
402979f8671SMichael Cree 		if (cpuc->current_idx[j] != PMC_NO_INDEX &&
403979f8671SMichael Cree 			cpuc->current_idx[j] != pe->hw.idx) {
404979f8671SMichael Cree 			alpha_perf_event_update(pe, &pe->hw, cpuc->current_idx[j], 0);
405979f8671SMichael Cree 			cpuc->current_idx[j] = PMC_NO_INDEX;
406979f8671SMichael Cree 		}
407979f8671SMichael Cree 	}
408979f8671SMichael Cree 
409979f8671SMichael Cree 	/* Assign to counters all unassigned events. */
410979f8671SMichael Cree 	cpuc->idx_mask = 0;
411979f8671SMichael Cree 	for (j = 0; j < cpuc->n_events; j++) {
412979f8671SMichael Cree 		struct perf_event *pe = cpuc->event[j];
413979f8671SMichael Cree 		struct hw_perf_event *hwc = &pe->hw;
414979f8671SMichael Cree 		int idx = hwc->idx;
415979f8671SMichael Cree 
416a4eaf7f1SPeter Zijlstra 		if (cpuc->current_idx[j] == PMC_NO_INDEX) {
417979f8671SMichael Cree 			alpha_perf_event_set_period(pe, hwc, idx);
418979f8671SMichael Cree 			cpuc->current_idx[j] = idx;
419a4eaf7f1SPeter Zijlstra 		}
420a4eaf7f1SPeter Zijlstra 
421a4eaf7f1SPeter Zijlstra 		if (!(hwc->state & PERF_HES_STOPPED))
422979f8671SMichael Cree 			cpuc->idx_mask |= (1<<cpuc->current_idx[j]);
423979f8671SMichael Cree 	}
424979f8671SMichael Cree 	cpuc->config = cpuc->event[0]->hw.config_base;
425979f8671SMichael Cree }
426979f8671SMichael Cree 
427979f8671SMichael Cree 
428979f8671SMichael Cree 
429979f8671SMichael Cree /* Schedule perf HW event on to PMU.
430979f8671SMichael Cree  *  - this function is called from outside this module via the pmu struct
431979f8671SMichael Cree  *    returned from perf event initialisation.
432979f8671SMichael Cree  */
alpha_pmu_add(struct perf_event * event,int flags)433a4eaf7f1SPeter Zijlstra static int alpha_pmu_add(struct perf_event *event, int flags)
434979f8671SMichael Cree {
4352999a4b3SChristoph Lameter 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
43665175c07SMichael Cree 	struct hw_perf_event *hwc = &event->hw;
437979f8671SMichael Cree 	int n0;
438979f8671SMichael Cree 	int ret;
43965175c07SMichael Cree 	unsigned long irq_flags;
440979f8671SMichael Cree 
441979f8671SMichael Cree 	/*
442979f8671SMichael Cree 	 * The Sparc code has the IRQ disable first followed by the perf
443979f8671SMichael Cree 	 * disable, however this can lead to an overflowed counter with the
444979f8671SMichael Cree 	 * PMI disabled on rare occasions.  The alpha_perf_event_update()
445979f8671SMichael Cree 	 * routine should detect this situation by noting a negative delta,
446979f8671SMichael Cree 	 * nevertheless we disable the PMCs first to enable a potential
447979f8671SMichael Cree 	 * final PMI to occur before we disable interrupts.
448979f8671SMichael Cree 	 */
44933696fc0SPeter Zijlstra 	perf_pmu_disable(event->pmu);
45065175c07SMichael Cree 	local_irq_save(irq_flags);
451979f8671SMichael Cree 
452979f8671SMichael Cree 	/* Default to error to be returned */
453979f8671SMichael Cree 	ret = -EAGAIN;
454979f8671SMichael Cree 
455979f8671SMichael Cree 	/* Insert event on to PMU and if successful modify ret to valid return */
456979f8671SMichael Cree 	n0 = cpuc->n_events;
457979f8671SMichael Cree 	if (n0 < alpha_pmu->num_pmcs) {
458979f8671SMichael Cree 		cpuc->event[n0] = event;
459979f8671SMichael Cree 		cpuc->evtype[n0] = event->hw.event_base;
460979f8671SMichael Cree 		cpuc->current_idx[n0] = PMC_NO_INDEX;
461979f8671SMichael Cree 
462979f8671SMichael Cree 		if (!alpha_check_constraints(cpuc->event, cpuc->evtype, n0+1)) {
463979f8671SMichael Cree 			cpuc->n_events++;
464979f8671SMichael Cree 			cpuc->n_added++;
465979f8671SMichael Cree 			ret = 0;
466979f8671SMichael Cree 		}
467979f8671SMichael Cree 	}
468979f8671SMichael Cree 
469a4eaf7f1SPeter Zijlstra 	hwc->state = PERF_HES_UPTODATE;
470a4eaf7f1SPeter Zijlstra 	if (!(flags & PERF_EF_START))
471a4eaf7f1SPeter Zijlstra 		hwc->state |= PERF_HES_STOPPED;
472a4eaf7f1SPeter Zijlstra 
47365175c07SMichael Cree 	local_irq_restore(irq_flags);
47433696fc0SPeter Zijlstra 	perf_pmu_enable(event->pmu);
475979f8671SMichael Cree 
476979f8671SMichael Cree 	return ret;
477979f8671SMichael Cree }
478979f8671SMichael Cree 
479979f8671SMichael Cree 
480979f8671SMichael Cree 
481979f8671SMichael Cree /* Disable performance monitoring unit
482979f8671SMichael Cree  *  - this function is called from outside this module via the pmu struct
483979f8671SMichael Cree  *    returned from perf event initialisation.
484979f8671SMichael Cree  */
alpha_pmu_del(struct perf_event * event,int flags)485a4eaf7f1SPeter Zijlstra static void alpha_pmu_del(struct perf_event *event, int flags)
486979f8671SMichael Cree {
4872999a4b3SChristoph Lameter 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
488979f8671SMichael Cree 	struct hw_perf_event *hwc = &event->hw;
48965175c07SMichael Cree 	unsigned long irq_flags;
490979f8671SMichael Cree 	int j;
491979f8671SMichael Cree 
49233696fc0SPeter Zijlstra 	perf_pmu_disable(event->pmu);
49365175c07SMichael Cree 	local_irq_save(irq_flags);
494979f8671SMichael Cree 
495979f8671SMichael Cree 	for (j = 0; j < cpuc->n_events; j++) {
496979f8671SMichael Cree 		if (event == cpuc->event[j]) {
497979f8671SMichael Cree 			int idx = cpuc->current_idx[j];
498979f8671SMichael Cree 
499979f8671SMichael Cree 			/* Shift remaining entries down into the existing
500979f8671SMichael Cree 			 * slot.
501979f8671SMichael Cree 			 */
502979f8671SMichael Cree 			while (++j < cpuc->n_events) {
503979f8671SMichael Cree 				cpuc->event[j - 1] = cpuc->event[j];
504979f8671SMichael Cree 				cpuc->evtype[j - 1] = cpuc->evtype[j];
505979f8671SMichael Cree 				cpuc->current_idx[j - 1] =
506979f8671SMichael Cree 					cpuc->current_idx[j];
507979f8671SMichael Cree 			}
508979f8671SMichael Cree 
509979f8671SMichael Cree 			/* Absorb the final count and turn off the event. */
510979f8671SMichael Cree 			alpha_perf_event_update(event, hwc, idx, 0);
511979f8671SMichael Cree 			perf_event_update_userpage(event);
512979f8671SMichael Cree 
513979f8671SMichael Cree 			cpuc->idx_mask &= ~(1UL<<idx);
514979f8671SMichael Cree 			cpuc->n_events--;
515979f8671SMichael Cree 			break;
516979f8671SMichael Cree 		}
517979f8671SMichael Cree 	}
518979f8671SMichael Cree 
51965175c07SMichael Cree 	local_irq_restore(irq_flags);
52033696fc0SPeter Zijlstra 	perf_pmu_enable(event->pmu);
521979f8671SMichael Cree }
522979f8671SMichael Cree 
523979f8671SMichael Cree 
alpha_pmu_read(struct perf_event * event)524979f8671SMichael Cree static void alpha_pmu_read(struct perf_event *event)
525979f8671SMichael Cree {
526979f8671SMichael Cree 	struct hw_perf_event *hwc = &event->hw;
527979f8671SMichael Cree 
528979f8671SMichael Cree 	alpha_perf_event_update(event, hwc, hwc->idx, 0);
529979f8671SMichael Cree }
530979f8671SMichael Cree 
531979f8671SMichael Cree 
alpha_pmu_stop(struct perf_event * event,int flags)532a4eaf7f1SPeter Zijlstra static void alpha_pmu_stop(struct perf_event *event, int flags)
533979f8671SMichael Cree {
534979f8671SMichael Cree 	struct hw_perf_event *hwc = &event->hw;
5352999a4b3SChristoph Lameter 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
536979f8671SMichael Cree 
537a4eaf7f1SPeter Zijlstra 	if (!(hwc->state & PERF_HES_STOPPED)) {
53865175c07SMichael Cree 		cpuc->idx_mask &= ~(1UL<<hwc->idx);
539a4eaf7f1SPeter Zijlstra 		hwc->state |= PERF_HES_STOPPED;
540a4eaf7f1SPeter Zijlstra 	}
541a4eaf7f1SPeter Zijlstra 
542a4eaf7f1SPeter Zijlstra 	if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
543a4eaf7f1SPeter Zijlstra 		alpha_perf_event_update(event, hwc, hwc->idx, 0);
544a4eaf7f1SPeter Zijlstra 		hwc->state |= PERF_HES_UPTODATE;
545a4eaf7f1SPeter Zijlstra 	}
546a4eaf7f1SPeter Zijlstra 
547a4eaf7f1SPeter Zijlstra 	if (cpuc->enabled)
54865175c07SMichael Cree 		wrperfmon(PERFMON_CMD_DISABLE, (1UL<<hwc->idx));
549a4eaf7f1SPeter Zijlstra }
550a4eaf7f1SPeter Zijlstra 
551a4eaf7f1SPeter Zijlstra 
alpha_pmu_start(struct perf_event * event,int flags)552a4eaf7f1SPeter Zijlstra static void alpha_pmu_start(struct perf_event *event, int flags)
553a4eaf7f1SPeter Zijlstra {
554a4eaf7f1SPeter Zijlstra 	struct hw_perf_event *hwc = &event->hw;
5552999a4b3SChristoph Lameter 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
556a4eaf7f1SPeter Zijlstra 
557a4eaf7f1SPeter Zijlstra 	if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
558a4eaf7f1SPeter Zijlstra 		return;
559a4eaf7f1SPeter Zijlstra 
560a4eaf7f1SPeter Zijlstra 	if (flags & PERF_EF_RELOAD) {
561a4eaf7f1SPeter Zijlstra 		WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
562a4eaf7f1SPeter Zijlstra 		alpha_perf_event_set_period(event, hwc, hwc->idx);
563a4eaf7f1SPeter Zijlstra 	}
564a4eaf7f1SPeter Zijlstra 
565a4eaf7f1SPeter Zijlstra 	hwc->state = 0;
566a4eaf7f1SPeter Zijlstra 
567979f8671SMichael Cree 	cpuc->idx_mask |= 1UL<<hwc->idx;
568a4eaf7f1SPeter Zijlstra 	if (cpuc->enabled)
569979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_ENABLE, (1UL<<hwc->idx));
570979f8671SMichael Cree }
571979f8671SMichael Cree 
572979f8671SMichael Cree 
573979f8671SMichael Cree /*
574979f8671SMichael Cree  * Check that CPU performance counters are supported.
575979f8671SMichael Cree  * - currently support EV67 and later CPUs.
576979f8671SMichael Cree  * - actually some later revisions of the EV6 have the same PMC model as the
577fc520525Sgushengxian  *     EV67 but we don't do sufficiently deep CPU detection to detect them.
578979f8671SMichael Cree  *     Bad luck to the very few people who might have one, I guess.
579979f8671SMichael Cree  */
supported_cpu(void)580979f8671SMichael Cree static int supported_cpu(void)
581979f8671SMichael Cree {
582979f8671SMichael Cree 	struct percpu_struct *cpu;
583979f8671SMichael Cree 	unsigned long cputype;
584979f8671SMichael Cree 
585979f8671SMichael Cree 	/* Get cpu type from HW */
586979f8671SMichael Cree 	cpu = (struct percpu_struct *)((char *)hwrpb + hwrpb->processor_offset);
587979f8671SMichael Cree 	cputype = cpu->type & 0xffffffff;
588979f8671SMichael Cree 	/* Include all of EV67, EV68, EV7, EV79 and EV69 as supported. */
589979f8671SMichael Cree 	return (cputype >= EV67_CPU) && (cputype <= EV69_CPU);
590979f8671SMichael Cree }
591979f8671SMichael Cree 
592979f8671SMichael Cree 
593979f8671SMichael Cree 
hw_perf_event_destroy(struct perf_event * event)594979f8671SMichael Cree static void hw_perf_event_destroy(struct perf_event *event)
595979f8671SMichael Cree {
596979f8671SMichael Cree 	/* Nothing to be done! */
597979f8671SMichael Cree 	return;
598979f8671SMichael Cree }
599979f8671SMichael Cree 
600979f8671SMichael Cree 
601979f8671SMichael Cree 
__hw_perf_event_init(struct perf_event * event)602979f8671SMichael Cree static int __hw_perf_event_init(struct perf_event *event)
603979f8671SMichael Cree {
604979f8671SMichael Cree 	struct perf_event_attr *attr = &event->attr;
605979f8671SMichael Cree 	struct hw_perf_event *hwc = &event->hw;
606979f8671SMichael Cree 	struct perf_event *evts[MAX_HWEVENTS];
607979f8671SMichael Cree 	unsigned long evtypes[MAX_HWEVENTS];
608979f8671SMichael Cree 	int idx_rubbish_bin[MAX_HWEVENTS];
609979f8671SMichael Cree 	int ev;
610979f8671SMichael Cree 	int n;
611979f8671SMichael Cree 
612979f8671SMichael Cree 	/* We only support a limited range of HARDWARE event types with one
613979f8671SMichael Cree 	 * only programmable via a RAW event type.
614979f8671SMichael Cree 	 */
615979f8671SMichael Cree 	if (attr->type == PERF_TYPE_HARDWARE) {
616979f8671SMichael Cree 		if (attr->config >= alpha_pmu->max_events)
617979f8671SMichael Cree 			return -EINVAL;
618979f8671SMichael Cree 		ev = alpha_pmu->event_map[attr->config];
619979f8671SMichael Cree 	} else if (attr->type == PERF_TYPE_HW_CACHE) {
620979f8671SMichael Cree 		return -EOPNOTSUPP;
621979f8671SMichael Cree 	} else if (attr->type == PERF_TYPE_RAW) {
6226e22f8f2SWill Deacon 		if (!alpha_pmu->raw_event_valid(attr->config))
6236e22f8f2SWill Deacon 			return -EINVAL;
6246e22f8f2SWill Deacon 		ev = attr->config;
625979f8671SMichael Cree 	} else {
626979f8671SMichael Cree 		return -EOPNOTSUPP;
627979f8671SMichael Cree 	}
628979f8671SMichael Cree 
629979f8671SMichael Cree 	if (ev < 0) {
630979f8671SMichael Cree 		return ev;
631979f8671SMichael Cree 	}
632979f8671SMichael Cree 
633979f8671SMichael Cree 	/*
634979f8671SMichael Cree 	 * We place the event type in event_base here and leave calculation
635979f8671SMichael Cree 	 * of the codes to programme the PMU for alpha_pmu_enable() because
636979f8671SMichael Cree 	 * it is only then we will know what HW events are actually
637979f8671SMichael Cree 	 * scheduled on to the PMU.  At that point the code to programme the
638979f8671SMichael Cree 	 * PMU is put into config_base and the PMC to use is placed into
639979f8671SMichael Cree 	 * idx.  We initialise idx (below) to PMC_NO_INDEX to indicate that
640979f8671SMichael Cree 	 * it is yet to be determined.
641979f8671SMichael Cree 	 */
642979f8671SMichael Cree 	hwc->event_base = ev;
643979f8671SMichael Cree 
644979f8671SMichael Cree 	/* Collect events in a group together suitable for calling
645979f8671SMichael Cree 	 * alpha_check_constraints() to verify that the group as a whole can
646979f8671SMichael Cree 	 * be scheduled on to the PMU.
647979f8671SMichael Cree 	 */
648979f8671SMichael Cree 	n = 0;
649979f8671SMichael Cree 	if (event->group_leader != event) {
650979f8671SMichael Cree 		n = collect_events(event->group_leader,
651979f8671SMichael Cree 				alpha_pmu->num_pmcs - 1,
652979f8671SMichael Cree 				evts, evtypes, idx_rubbish_bin);
653979f8671SMichael Cree 		if (n < 0)
654979f8671SMichael Cree 			return -EINVAL;
655979f8671SMichael Cree 	}
656979f8671SMichael Cree 	evtypes[n] = hwc->event_base;
657979f8671SMichael Cree 	evts[n] = event;
658979f8671SMichael Cree 
659979f8671SMichael Cree 	if (alpha_check_constraints(evts, evtypes, n + 1))
660979f8671SMichael Cree 		return -EINVAL;
661979f8671SMichael Cree 
662979f8671SMichael Cree 	/* Indicate that PMU config and idx are yet to be determined. */
663979f8671SMichael Cree 	hwc->config_base = 0;
664979f8671SMichael Cree 	hwc->idx = PMC_NO_INDEX;
665979f8671SMichael Cree 
666979f8671SMichael Cree 	event->destroy = hw_perf_event_destroy;
667979f8671SMichael Cree 
668979f8671SMichael Cree 	/*
669979f8671SMichael Cree 	 * Most architectures reserve the PMU for their use at this point.
670979f8671SMichael Cree 	 * As there is no existing mechanism to arbitrate usage and there
671979f8671SMichael Cree 	 * appears to be no other user of the Alpha PMU we just assume
672979f8671SMichael Cree 	 * that we can just use it, hence a NO-OP here.
673979f8671SMichael Cree 	 *
674979f8671SMichael Cree 	 * Maybe an alpha_reserve_pmu() routine should be implemented but is
675979f8671SMichael Cree 	 * anything else ever going to use it?
676979f8671SMichael Cree 	 */
677979f8671SMichael Cree 
678979f8671SMichael Cree 	if (!hwc->sample_period) {
679979f8671SMichael Cree 		hwc->sample_period = alpha_pmu->pmc_max_period[0];
680979f8671SMichael Cree 		hwc->last_period = hwc->sample_period;
6817b598cddSMichael Cree 		local64_set(&hwc->period_left, hwc->sample_period);
682979f8671SMichael Cree 	}
683979f8671SMichael Cree 
684979f8671SMichael Cree 	return 0;
685979f8671SMichael Cree }
686979f8671SMichael Cree 
687b0a873ebSPeter Zijlstra /*
688b0a873ebSPeter Zijlstra  * Main entry point to initialise a HW performance event.
689b0a873ebSPeter Zijlstra  */
alpha_pmu_event_init(struct perf_event * event)690b0a873ebSPeter Zijlstra static int alpha_pmu_event_init(struct perf_event *event)
691b0a873ebSPeter Zijlstra {
6922481c5faSStephane Eranian 	/* does not support taken branch sampling */
6932481c5faSStephane Eranian 	if (has_branch_stack(event))
6942481c5faSStephane Eranian 		return -EOPNOTSUPP;
6952481c5faSStephane Eranian 
696b0a873ebSPeter Zijlstra 	switch (event->attr.type) {
697b0a873ebSPeter Zijlstra 	case PERF_TYPE_RAW:
698b0a873ebSPeter Zijlstra 	case PERF_TYPE_HARDWARE:
699b0a873ebSPeter Zijlstra 	case PERF_TYPE_HW_CACHE:
700b0a873ebSPeter Zijlstra 		break;
701b0a873ebSPeter Zijlstra 
702b0a873ebSPeter Zijlstra 	default:
703b0a873ebSPeter Zijlstra 		return -ENOENT;
704b0a873ebSPeter Zijlstra 	}
705b0a873ebSPeter Zijlstra 
706b0a873ebSPeter Zijlstra 	if (!alpha_pmu)
707b0a873ebSPeter Zijlstra 		return -ENODEV;
708b0a873ebSPeter Zijlstra 
709b0a873ebSPeter Zijlstra 	/* Do the real initialisation work. */
710*d7cf43edSMinghao Chi 	return __hw_perf_event_init(event);
711b0a873ebSPeter Zijlstra }
712b0a873ebSPeter Zijlstra 
713979f8671SMichael Cree /*
714979f8671SMichael Cree  * Main entry point - enable HW performance counters.
715979f8671SMichael Cree  */
alpha_pmu_enable(struct pmu * pmu)716a4eaf7f1SPeter Zijlstra static void alpha_pmu_enable(struct pmu *pmu)
717979f8671SMichael Cree {
7182999a4b3SChristoph Lameter 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
719979f8671SMichael Cree 
720979f8671SMichael Cree 	if (cpuc->enabled)
721979f8671SMichael Cree 		return;
722979f8671SMichael Cree 
723979f8671SMichael Cree 	cpuc->enabled = 1;
724979f8671SMichael Cree 	barrier();
725979f8671SMichael Cree 
726979f8671SMichael Cree 	if (cpuc->n_events > 0) {
727979f8671SMichael Cree 		/* Update cpuc with information from any new scheduled events. */
728979f8671SMichael Cree 		maybe_change_configuration(cpuc);
729979f8671SMichael Cree 
730979f8671SMichael Cree 		/* Start counting the desired events. */
731979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_LOGGING_OPTIONS, EV67_PCTR_MODE_AGGREGATE);
732979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_DESIRED_EVENTS, cpuc->config);
733979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
734979f8671SMichael Cree 	}
735979f8671SMichael Cree }
736979f8671SMichael Cree 
737979f8671SMichael Cree 
738979f8671SMichael Cree /*
739979f8671SMichael Cree  * Main entry point - disable HW performance counters.
740979f8671SMichael Cree  */
741979f8671SMichael Cree 
alpha_pmu_disable(struct pmu * pmu)742a4eaf7f1SPeter Zijlstra static void alpha_pmu_disable(struct pmu *pmu)
743979f8671SMichael Cree {
7442999a4b3SChristoph Lameter 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
745979f8671SMichael Cree 
746979f8671SMichael Cree 	if (!cpuc->enabled)
747979f8671SMichael Cree 		return;
748979f8671SMichael Cree 
749979f8671SMichael Cree 	cpuc->enabled = 0;
750979f8671SMichael Cree 	cpuc->n_added = 0;
751979f8671SMichael Cree 
752979f8671SMichael Cree 	wrperfmon(PERFMON_CMD_DISABLE, cpuc->idx_mask);
753979f8671SMichael Cree }
754979f8671SMichael Cree 
75533696fc0SPeter Zijlstra static struct pmu pmu = {
756a4eaf7f1SPeter Zijlstra 	.pmu_enable	= alpha_pmu_enable,
757a4eaf7f1SPeter Zijlstra 	.pmu_disable	= alpha_pmu_disable,
75833696fc0SPeter Zijlstra 	.event_init	= alpha_pmu_event_init,
759a4eaf7f1SPeter Zijlstra 	.add		= alpha_pmu_add,
760a4eaf7f1SPeter Zijlstra 	.del		= alpha_pmu_del,
761a4eaf7f1SPeter Zijlstra 	.start		= alpha_pmu_start,
762a4eaf7f1SPeter Zijlstra 	.stop		= alpha_pmu_stop,
76333696fc0SPeter Zijlstra 	.read		= alpha_pmu_read,
7646dd273f4SAndrew Murray 	.capabilities	= PERF_PMU_CAP_NO_EXCLUDE,
76533696fc0SPeter Zijlstra };
76633696fc0SPeter Zijlstra 
767979f8671SMichael Cree 
768979f8671SMichael Cree /*
769979f8671SMichael Cree  * Main entry point - don't know when this is called but it
770979f8671SMichael Cree  * obviously dumps debug info.
771979f8671SMichael Cree  */
perf_event_print_debug(void)772979f8671SMichael Cree void perf_event_print_debug(void)
773979f8671SMichael Cree {
774979f8671SMichael Cree 	unsigned long flags;
775979f8671SMichael Cree 	unsigned long pcr;
776979f8671SMichael Cree 	int pcr0, pcr1;
777979f8671SMichael Cree 	int cpu;
778979f8671SMichael Cree 
779979f8671SMichael Cree 	if (!supported_cpu())
780979f8671SMichael Cree 		return;
781979f8671SMichael Cree 
782979f8671SMichael Cree 	local_irq_save(flags);
783979f8671SMichael Cree 
784979f8671SMichael Cree 	cpu = smp_processor_id();
785979f8671SMichael Cree 
786979f8671SMichael Cree 	pcr = wrperfmon(PERFMON_CMD_READ, 0);
787979f8671SMichael Cree 	pcr0 = (pcr >> alpha_pmu->pmc_count_shift[0]) & alpha_pmu->pmc_count_mask[0];
788979f8671SMichael Cree 	pcr1 = (pcr >> alpha_pmu->pmc_count_shift[1]) & alpha_pmu->pmc_count_mask[1];
789979f8671SMichael Cree 
790979f8671SMichael Cree 	pr_info("CPU#%d: PCTR0[%06x] PCTR1[%06x]\n", cpu, pcr0, pcr1);
791979f8671SMichael Cree 
792979f8671SMichael Cree 	local_irq_restore(flags);
793979f8671SMichael Cree }
794979f8671SMichael Cree 
795979f8671SMichael Cree 
796979f8671SMichael Cree /*
797979f8671SMichael Cree  * Performance Monitoring Interrupt Service Routine called when a PMC
798979f8671SMichael Cree  * overflows.  The PMC that overflowed is passed in la_ptr.
799979f8671SMichael Cree  */
alpha_perf_event_irq_handler(unsigned long la_ptr,struct pt_regs * regs)800979f8671SMichael Cree static void alpha_perf_event_irq_handler(unsigned long la_ptr,
801979f8671SMichael Cree 					struct pt_regs *regs)
802979f8671SMichael Cree {
803979f8671SMichael Cree 	struct cpu_hw_events *cpuc;
804979f8671SMichael Cree 	struct perf_sample_data data;
805979f8671SMichael Cree 	struct perf_event *event;
806979f8671SMichael Cree 	struct hw_perf_event *hwc;
807979f8671SMichael Cree 	int idx, j;
808979f8671SMichael Cree 
8092999a4b3SChristoph Lameter 	__this_cpu_inc(irq_pmi_count);
8102999a4b3SChristoph Lameter 	cpuc = this_cpu_ptr(&cpu_hw_events);
811979f8671SMichael Cree 
812979f8671SMichael Cree 	/* Completely counting through the PMC's period to trigger a new PMC
813979f8671SMichael Cree 	 * overflow interrupt while in this interrupt routine is utterly
814979f8671SMichael Cree 	 * disastrous!  The EV6 and EV67 counters are sufficiently large to
815979f8671SMichael Cree 	 * prevent this but to be really sure disable the PMCs.
816979f8671SMichael Cree 	 */
817979f8671SMichael Cree 	wrperfmon(PERFMON_CMD_DISABLE, cpuc->idx_mask);
818979f8671SMichael Cree 
819979f8671SMichael Cree 	/* la_ptr is the counter that overflowed. */
82015ac9a39SPeter Zijlstra 	if (unlikely(la_ptr >= alpha_pmu->num_pmcs)) {
821979f8671SMichael Cree 		/* This should never occur! */
822979f8671SMichael Cree 		irq_err_count++;
823a7590d68SKefeng Wang 		pr_warn("PMI: silly index %ld\n", la_ptr);
824979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
825979f8671SMichael Cree 		return;
826979f8671SMichael Cree 	}
827979f8671SMichael Cree 
828979f8671SMichael Cree 	idx = la_ptr;
829979f8671SMichael Cree 
830979f8671SMichael Cree 	for (j = 0; j < cpuc->n_events; j++) {
831979f8671SMichael Cree 		if (cpuc->current_idx[j] == idx)
832979f8671SMichael Cree 			break;
833979f8671SMichael Cree 	}
834979f8671SMichael Cree 
835979f8671SMichael Cree 	if (unlikely(j == cpuc->n_events)) {
836979f8671SMichael Cree 		/* This can occur if the event is disabled right on a PMC overflow. */
837979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
838979f8671SMichael Cree 		return;
839979f8671SMichael Cree 	}
840979f8671SMichael Cree 
841979f8671SMichael Cree 	event = cpuc->event[j];
842979f8671SMichael Cree 
843979f8671SMichael Cree 	if (unlikely(!event)) {
844979f8671SMichael Cree 		/* This should never occur! */
845979f8671SMichael Cree 		irq_err_count++;
846a7590d68SKefeng Wang 		pr_warn("PMI: No event at index %d!\n", idx);
847979f8671SMichael Cree 		wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
848979f8671SMichael Cree 		return;
849979f8671SMichael Cree 	}
850979f8671SMichael Cree 
851979f8671SMichael Cree 	hwc = &event->hw;
852979f8671SMichael Cree 	alpha_perf_event_update(event, hwc, idx, alpha_pmu->pmc_max_period[idx]+1);
853fd0d000bSRobert Richter 	perf_sample_data_init(&data, 0, hwc->last_period);
854979f8671SMichael Cree 
855979f8671SMichael Cree 	if (alpha_perf_event_set_period(event, hwc, idx)) {
856a8b0ca17SPeter Zijlstra 		if (perf_event_overflow(event, &data, regs)) {
857979f8671SMichael Cree 			/* Interrupts coming too quickly; "throttle" the
858979f8671SMichael Cree 			 * counter, i.e., disable it for a little while.
859979f8671SMichael Cree 			 */
86065175c07SMichael Cree 			alpha_pmu_stop(event, 0);
861979f8671SMichael Cree 		}
862979f8671SMichael Cree 	}
863979f8671SMichael Cree 	wrperfmon(PERFMON_CMD_ENABLE, cpuc->idx_mask);
864979f8671SMichael Cree 
865979f8671SMichael Cree 	return;
866979f8671SMichael Cree }
867979f8671SMichael Cree 
868979f8671SMichael Cree 
869979f8671SMichael Cree 
870979f8671SMichael Cree /*
871979f8671SMichael Cree  * Init call to initialise performance events at kernel startup.
872979f8671SMichael Cree  */
init_hw_perf_events(void)873004417a6SPeter Zijlstra int __init init_hw_perf_events(void)
874979f8671SMichael Cree {
875979f8671SMichael Cree 	pr_info("Performance events: ");
876979f8671SMichael Cree 
877979f8671SMichael Cree 	if (!supported_cpu()) {
878979f8671SMichael Cree 		pr_cont("No support for your CPU.\n");
879004417a6SPeter Zijlstra 		return 0;
880979f8671SMichael Cree 	}
881979f8671SMichael Cree 
882979f8671SMichael Cree 	pr_cont("Supported CPU type!\n");
883979f8671SMichael Cree 
884979f8671SMichael Cree 	/* Override performance counter IRQ vector */
885979f8671SMichael Cree 
886979f8671SMichael Cree 	perf_irq = alpha_perf_event_irq_handler;
887979f8671SMichael Cree 
888979f8671SMichael Cree 	/* And set up PMU specification */
889979f8671SMichael Cree 	alpha_pmu = &ev67_pmu;
890b0a873ebSPeter Zijlstra 
8912e80a82aSPeter Zijlstra 	perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
892979f8671SMichael Cree 
893004417a6SPeter Zijlstra 	return 0;
894004417a6SPeter Zijlstra }
895004417a6SPeter Zijlstra early_initcall(init_hw_perf_events);
896