xref: /OK3568_Linux_fs/kernel/arch/alpha/oprofile/op_model_ev5.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /**
2*4882a593Smuzhiyun  * @file arch/alpha/oprofile/op_model_ev5.c
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * @remark Copyright 2002 OProfile authors
5*4882a593Smuzhiyun  * @remark Read the file COPYING
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * @author Richard Henderson <rth@twiddle.net>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/oprofile.h>
11*4882a593Smuzhiyun #include <linux/smp.h>
12*4882a593Smuzhiyun #include <asm/ptrace.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include "op_impl.h"
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /* Compute all of the registers in preparation for enabling profiling.
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun    The 21164 (EV5) and 21164PC (PCA65) vary in the bit placement and
20*4882a593Smuzhiyun    meaning of the "CBOX" events.  Given that we don't care about meaning
21*4882a593Smuzhiyun    at this point, arrange for the difference in bit placement to be
22*4882a593Smuzhiyun    handled by common code.  */
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun static void
common_reg_setup(struct op_register_config * reg,struct op_counter_config * ctr,struct op_system_config * sys,int cbox1_ofs,int cbox2_ofs)25*4882a593Smuzhiyun common_reg_setup(struct op_register_config *reg,
26*4882a593Smuzhiyun 		 struct op_counter_config *ctr,
27*4882a593Smuzhiyun 		 struct op_system_config *sys,
28*4882a593Smuzhiyun 		 int cbox1_ofs, int cbox2_ofs)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	int i, ctl, reset, need_reset;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	/* Select desired events.  The event numbers are selected such
33*4882a593Smuzhiyun 	   that they map directly into the event selection fields:
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 		PCSEL0:	0, 1
36*4882a593Smuzhiyun 		PCSEL1:	24-39
37*4882a593Smuzhiyun 		 CBOX1: 40-47
38*4882a593Smuzhiyun 		PCSEL2: 48-63
39*4882a593Smuzhiyun 		 CBOX2: 64-71
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	   There are two special cases, in that CYCLES can be measured
42*4882a593Smuzhiyun 	   on PCSEL[02], and SCACHE_WRITE can be measured on CBOX[12].
43*4882a593Smuzhiyun 	   These event numbers are canonicalizes to their first appearance.  */
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	ctl = 0;
46*4882a593Smuzhiyun 	for (i = 0; i < 3; ++i) {
47*4882a593Smuzhiyun 		unsigned long event = ctr[i].event;
48*4882a593Smuzhiyun 		if (!ctr[i].enabled)
49*4882a593Smuzhiyun 			continue;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 		/* Remap the duplicate events, as described above.  */
52*4882a593Smuzhiyun 		if (i == 2) {
53*4882a593Smuzhiyun 			if (event == 0)
54*4882a593Smuzhiyun 				event = 12+48;
55*4882a593Smuzhiyun 			else if (event == 2+41)
56*4882a593Smuzhiyun 				event = 4+65;
57*4882a593Smuzhiyun 		}
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 		/* Convert the event numbers onto mux_select bit mask.  */
60*4882a593Smuzhiyun 		if (event < 2)
61*4882a593Smuzhiyun 			ctl |= event << 31;
62*4882a593Smuzhiyun 		else if (event < 24)
63*4882a593Smuzhiyun 			/* error */;
64*4882a593Smuzhiyun 		else if (event < 40)
65*4882a593Smuzhiyun 			ctl |= (event - 24) << 4;
66*4882a593Smuzhiyun 		else if (event < 48)
67*4882a593Smuzhiyun 			ctl |= (event - 40) << cbox1_ofs | 15 << 4;
68*4882a593Smuzhiyun 		else if (event < 64)
69*4882a593Smuzhiyun 			ctl |= event - 48;
70*4882a593Smuzhiyun 		else if (event < 72)
71*4882a593Smuzhiyun 			ctl |= (event - 64) << cbox2_ofs | 15;
72*4882a593Smuzhiyun 	}
73*4882a593Smuzhiyun 	reg->mux_select = ctl;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	/* Select processor mode.  */
76*4882a593Smuzhiyun 	/* ??? Need to come up with some mechanism to trace only selected
77*4882a593Smuzhiyun 	   processes.  For now select from pal, kernel and user mode.  */
78*4882a593Smuzhiyun 	ctl = 0;
79*4882a593Smuzhiyun 	ctl |= !sys->enable_pal << 9;
80*4882a593Smuzhiyun 	ctl |= !sys->enable_kernel << 8;
81*4882a593Smuzhiyun 	ctl |= !sys->enable_user << 30;
82*4882a593Smuzhiyun 	reg->proc_mode = ctl;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	/* Select interrupt frequencies.  Take the interrupt count selected
85*4882a593Smuzhiyun 	   by the user, and map it onto one of the possible counter widths.
86*4882a593Smuzhiyun 	   If the user value is in between, compute a value to which the
87*4882a593Smuzhiyun 	   counter is reset at each interrupt.  */
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	ctl = reset = need_reset = 0;
90*4882a593Smuzhiyun 	for (i = 0; i < 3; ++i) {
91*4882a593Smuzhiyun 		unsigned long max, hilo, count = ctr[i].count;
92*4882a593Smuzhiyun 		if (!ctr[i].enabled)
93*4882a593Smuzhiyun 			continue;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 		if (count <= 256)
96*4882a593Smuzhiyun 			count = 256, hilo = 3, max = 256;
97*4882a593Smuzhiyun 		else {
98*4882a593Smuzhiyun 			max = (i == 2 ? 16384 : 65536);
99*4882a593Smuzhiyun 			hilo = 2;
100*4882a593Smuzhiyun 			if (count > max)
101*4882a593Smuzhiyun 				count = max;
102*4882a593Smuzhiyun 		}
103*4882a593Smuzhiyun 		ctr[i].count = count;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 		ctl |= hilo << (8 - i*2);
106*4882a593Smuzhiyun 		reset |= (max - count) << (48 - 16*i);
107*4882a593Smuzhiyun 		if (count != max)
108*4882a593Smuzhiyun 			need_reset |= 1 << i;
109*4882a593Smuzhiyun 	}
110*4882a593Smuzhiyun 	reg->freq = ctl;
111*4882a593Smuzhiyun 	reg->reset_values = reset;
112*4882a593Smuzhiyun 	reg->need_reset = need_reset;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun static void
ev5_reg_setup(struct op_register_config * reg,struct op_counter_config * ctr,struct op_system_config * sys)116*4882a593Smuzhiyun ev5_reg_setup(struct op_register_config *reg,
117*4882a593Smuzhiyun 	      struct op_counter_config *ctr,
118*4882a593Smuzhiyun 	      struct op_system_config *sys)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	common_reg_setup(reg, ctr, sys, 19, 22);
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun static void
pca56_reg_setup(struct op_register_config * reg,struct op_counter_config * ctr,struct op_system_config * sys)124*4882a593Smuzhiyun pca56_reg_setup(struct op_register_config *reg,
125*4882a593Smuzhiyun 	        struct op_counter_config *ctr,
126*4882a593Smuzhiyun 	        struct op_system_config *sys)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	common_reg_setup(reg, ctr, sys, 8, 11);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun /* Program all of the registers in preparation for enabling profiling.  */
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun static void
ev5_cpu_setup(void * x)134*4882a593Smuzhiyun ev5_cpu_setup (void *x)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	struct op_register_config *reg = x;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	wrperfmon(2, reg->mux_select);
139*4882a593Smuzhiyun 	wrperfmon(3, reg->proc_mode);
140*4882a593Smuzhiyun 	wrperfmon(4, reg->freq);
141*4882a593Smuzhiyun 	wrperfmon(6, reg->reset_values);
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun /* CTR is a counter for which the user has requested an interrupt count
145*4882a593Smuzhiyun    in between one of the widths selectable in hardware.  Reset the count
146*4882a593Smuzhiyun    for CTR to the value stored in REG->RESET_VALUES.
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun    For EV5, this means disabling profiling, reading the current values,
149*4882a593Smuzhiyun    masking in the value for the desired register, writing, then turning
150*4882a593Smuzhiyun    profiling back on.
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun    This can be streamlined if profiling is only enabled for user mode.
153*4882a593Smuzhiyun    In that case we know that the counters are not currently incrementing
154*4882a593Smuzhiyun    (due to being in kernel mode).  */
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun static void
ev5_reset_ctr(struct op_register_config * reg,unsigned long ctr)157*4882a593Smuzhiyun ev5_reset_ctr(struct op_register_config *reg, unsigned long ctr)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun 	unsigned long values, mask, not_pk, reset_values;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	mask = (ctr == 0 ? 0xfffful << 48
162*4882a593Smuzhiyun 	        : ctr == 1 ? 0xfffful << 32
163*4882a593Smuzhiyun 		: 0x3fff << 16);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	not_pk = 1 << 9 | 1 << 8;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	reset_values = reg->reset_values;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	if ((reg->proc_mode & not_pk) == not_pk) {
170*4882a593Smuzhiyun 		values = wrperfmon(5, 0);
171*4882a593Smuzhiyun 		values = (reset_values & mask) | (values & ~mask & -2);
172*4882a593Smuzhiyun 		wrperfmon(6, values);
173*4882a593Smuzhiyun 	} else {
174*4882a593Smuzhiyun 		wrperfmon(0, -1);
175*4882a593Smuzhiyun 		values = wrperfmon(5, 0);
176*4882a593Smuzhiyun 		values = (reset_values & mask) | (values & ~mask & -2);
177*4882a593Smuzhiyun 		wrperfmon(6, values);
178*4882a593Smuzhiyun 		wrperfmon(1, reg->enable);
179*4882a593Smuzhiyun 	}
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun static void
ev5_handle_interrupt(unsigned long which,struct pt_regs * regs,struct op_counter_config * ctr)183*4882a593Smuzhiyun ev5_handle_interrupt(unsigned long which, struct pt_regs *regs,
184*4882a593Smuzhiyun 		     struct op_counter_config *ctr)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun 	/* Record the sample.  */
187*4882a593Smuzhiyun 	oprofile_add_sample(regs, which);
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun struct op_axp_model op_model_ev5 = {
192*4882a593Smuzhiyun 	.reg_setup		= ev5_reg_setup,
193*4882a593Smuzhiyun 	.cpu_setup		= ev5_cpu_setup,
194*4882a593Smuzhiyun 	.reset_ctr		= ev5_reset_ctr,
195*4882a593Smuzhiyun 	.handle_interrupt	= ev5_handle_interrupt,
196*4882a593Smuzhiyun 	.cpu_type		= "alpha/ev5",
197*4882a593Smuzhiyun 	.num_counters		= 3,
198*4882a593Smuzhiyun 	.can_set_proc_mode	= 1,
199*4882a593Smuzhiyun };
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun struct op_axp_model op_model_pca56 = {
202*4882a593Smuzhiyun 	.reg_setup		= pca56_reg_setup,
203*4882a593Smuzhiyun 	.cpu_setup		= ev5_cpu_setup,
204*4882a593Smuzhiyun 	.reset_ctr		= ev5_reset_ctr,
205*4882a593Smuzhiyun 	.handle_interrupt	= ev5_handle_interrupt,
206*4882a593Smuzhiyun 	.cpu_type		= "alpha/pca56",
207*4882a593Smuzhiyun 	.num_counters		= 3,
208*4882a593Smuzhiyun 	.can_set_proc_mode	= 1,
209*4882a593Smuzhiyun };
210