perfmon.c revision 17395
1/*
2 * Copyright 1996 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission.  M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose.  It is provided "as is" without express or implied
14 * warranty.
15 *
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	$Id: perfmon.c,v 1.6 1996/06/12 05:02:51 gpalmer Exp $
30 */
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/fcntl.h>
35#include <sys/ioccom.h>
36
37#include <machine/cpu.h>
38#include <machine/cputypes.h>
39#include <machine/clock.h>
40#include <machine/perfmon.h>
41
42static int perfmon_inuse;
43static int perfmon_cpuok;
44static int msr_ctl[NPMC];
45static int msr_pmc[NPMC];
46static unsigned int ctl_shadow[NPMC];
47static quad_t pmc_shadow[NPMC];	/* used when ctr is stopped on P5 */
48static int (*writectl)(int);
49static int writectl5(int);
50static int writectl6(int);
51
52/*
53 * Must be called after cpu_class is set up.
54 */
55void
56perfmon_init(void)
57{
58	switch(cpu_class) {
59	case CPUCLASS_586:
60		perfmon_cpuok = 1;
61		msr_ctl[0] = 0x11;
62		msr_ctl[1] = 0x11;
63		msr_pmc[0] = 0x12;
64		msr_pmc[1] = 0x13;
65		writectl = writectl5;
66		break;
67	case CPUCLASS_686:
68		perfmon_cpuok = 1;
69		msr_ctl[0] = 0x186;
70		msr_ctl[1] = 0x187;
71		msr_pmc[0] = 0xc1;
72		msr_pmc[1] = 0xc2;
73		writectl = writectl6;
74		break;
75
76	default:
77		perfmon_cpuok = 0;
78		break;
79	}
80}
81
82int
83perfmon_avail(void)
84{
85	return perfmon_cpuok;
86}
87
88int
89perfmon_setup(int pmc, unsigned int control)
90{
91	if (pmc < 0 || pmc >= NPMC)
92		return EINVAL;
93
94	perfmon_inuse |= (1 << pmc);
95	control &= ~(PMCF_SYS_FLAGS << 16);
96	disable_intr();
97	ctl_shadow[pmc] = control;
98	writectl(pmc);
99	wrmsr(msr_pmc[pmc], pmc_shadow[pmc] = 0);
100	enable_intr();
101	return 0;
102}
103
104int
105perfmon_get(int pmc, unsigned int *control)
106{
107	if (pmc < 0 || pmc >= NPMC)
108		return EINVAL;
109
110	if (perfmon_inuse & (1 << pmc)) {
111		*control = ctl_shadow[pmc];
112		return 0;
113	}
114	return EBUSY;		/* XXX reversed sense */
115}
116
117int
118perfmon_fini(int pmc)
119{
120	if (pmc < 0 || pmc >= NPMC)
121		return EINVAL;
122
123	if (perfmon_inuse & (1 << pmc)) {
124		perfmon_stop(pmc);
125		ctl_shadow[pmc] = 0;
126		perfmon_inuse &= ~(1 << pmc);
127		return 0;
128	}
129	return EBUSY;		/* XXX reversed sense */
130}
131
132int
133perfmon_start(int pmc)
134{
135	if (pmc < 0 || pmc >= NPMC)
136		return EINVAL;
137
138	if (perfmon_inuse & (1 << pmc)) {
139		disable_intr();
140		ctl_shadow[pmc] |= (PMCF_EN << 16);
141		wrmsr(msr_pmc[pmc], pmc_shadow[pmc]);
142		writectl(pmc);
143		enable_intr();
144		return 0;
145	}
146	return EBUSY;
147}
148
149int
150perfmon_stop(int pmc)
151{
152	if (pmc < 0 || pmc >= NPMC)
153		return EINVAL;
154
155	if (perfmon_inuse & (1 << pmc)) {
156		disable_intr();
157		pmc_shadow[pmc] = rdmsr(msr_pmc[pmc]);
158		ctl_shadow[pmc] &= ~(PMCF_EN << 16);
159		writectl(pmc);
160		enable_intr();
161		return 0;
162	}
163	return EBUSY;
164}
165
166int
167perfmon_read(int pmc, quad_t *val)
168{
169	if (pmc < 0 || pmc >= NPMC)
170		return EINVAL;
171
172	if (perfmon_inuse & (1 << pmc)) {
173		if (ctl_shadow[pmc] & (PMCF_EN << 16))
174			*val = rdmsr(msr_pmc[pmc]);
175		else
176			*val = pmc_shadow[pmc];
177		return 0;
178	}
179
180	return EBUSY;
181}
182
183int
184perfmon_reset(int pmc)
185{
186	if (pmc < 0 || pmc >= NPMC)
187		return EINVAL;
188
189	if (perfmon_inuse & (1 << pmc)) {
190		wrmsr(msr_pmc[pmc], pmc_shadow[pmc] = 0);
191		return 0;
192	}
193	return EBUSY;
194}
195
196/*
197 * Unfortunately, the performance-monitoring registers are laid out
198 * differently in the P5 and P6.  We keep everything in P6 format
199 * internally (except for the event code), and convert to P5
200 * format as needed on those CPUs.  The writectl function pointer
201 * is set up to point to one of these functions by perfmon_init().
202 */
203int
204writectl6(int pmc)
205{
206	if (pmc > 0 && !(ctl_shadow[pmc] & (PMCF_EN << 16))) {
207		wrmsr(msr_ctl[pmc], 0);
208	} else {
209		wrmsr(msr_ctl[pmc], ctl_shadow[pmc]);
210	}
211	return 0;
212}
213
214#define	P5FLAG_P	0x200
215#define	P5FLAG_E	0x100
216#define	P5FLAG_USR	0x80
217#define	P5FLAG_OS	0x40
218
219int
220writectl5(int pmc)
221{
222	quad_t newval = 0;
223
224	if (ctl_shadow[1] & (PMCF_EN << 16)) {
225		if (ctl_shadow[1] & (PMCF_USR << 16))
226			newval |= P5FLAG_USR << 16;
227		if (ctl_shadow[1] & (PMCF_OS << 16))
228			newval |= P5FLAG_OS << 16;
229		if (!(ctl_shadow[1] & (PMCF_E << 16)))
230			newval |= P5FLAG_E << 16;
231		newval |= (ctl_shadow[1] & 0x3f) << 16;
232	}
233	if (ctl_shadow[0] & (PMCF_EN << 16)) {
234		if (ctl_shadow[0] & (PMCF_USR << 16))
235			newval |= P5FLAG_USR;
236		if (ctl_shadow[0] & (PMCF_OS << 16))
237			newval |= P5FLAG_OS;
238		if (!(ctl_shadow[0] & (PMCF_E << 16)))
239			newval |= P5FLAG_E;
240		newval |= ctl_shadow[0] & 0x3f;
241	}
242
243	wrmsr(msr_ctl[0], newval);
244	return 0;		/* XXX should check for unimplemented bits */
245}
246
247/*
248 * Now the user-mode interface, called from a subdevice of mem.c.
249 */
250static int writer;
251static int writerpmc;
252
253int
254perfmon_open(dev_t dev, int flags, int fmt, struct proc *p)
255{
256	if (!perfmon_cpuok)
257		return ENXIO;
258
259	if (flags & FWRITE) {
260		if (writer) {
261			return EBUSY;
262		} else {
263			writer = 1;
264			writerpmc = 0;
265		}
266	}
267	return 0;
268}
269
270int
271perfmon_close(dev_t dev, int flags, int fmt, struct proc *p)
272{
273	if (flags & FWRITE) {
274		int i;
275
276		for (i = 0; i < NPMC; i++) {
277			if (writerpmc & (1 << i))
278				perfmon_fini(i);
279		}
280		writer = 0;
281	}
282	return 0;
283}
284
285int
286perfmon_ioctl(dev_t dev, int cmd, caddr_t param, int flags, struct proc *p)
287{
288	struct pmc *pmc;
289	struct pmc_data *pmcd;
290	struct pmc_tstamp *pmct;
291	int *ip;
292	int rv;
293
294	switch(cmd) {
295	case PMIOSETUP:
296		if (!(flags & FWRITE))
297			return EPERM;
298		pmc = (struct pmc *)param;
299
300		rv = perfmon_setup(pmc->pmc_num, pmc->pmc_val);
301		if (!rv) {
302			writerpmc |= (1 << pmc->pmc_num);
303		}
304		break;
305
306	case PMIOGET:
307		pmc = (struct pmc *)param;
308		rv = perfmon_get(pmc->pmc_num, &pmc->pmc_val);
309		break;
310
311	case PMIOSTART:
312		if (!(flags & FWRITE))
313			return EPERM;
314
315		ip = (int *)param;
316		rv = perfmon_start(*ip);
317		break;
318
319	case PMIOSTOP:
320		if (!(flags & FWRITE))
321			return EPERM;
322
323		ip = (int *)param;
324		rv = perfmon_stop(*ip);
325		break;
326
327	case PMIORESET:
328		if (!(flags & FWRITE))
329			return EPERM;
330
331		ip = (int *)param;
332		rv = perfmon_reset(*ip);
333		break;
334
335	case PMIOREAD:
336		pmcd = (struct pmc_data *)param;
337		rv = perfmon_read(pmcd->pmcd_num, &pmcd->pmcd_value);
338		break;
339
340	case PMIOTSTAMP:
341		pmct = (struct pmc_tstamp *)param;
342		/* XXX interface loses precision. */
343		pmct->pmct_rate = i586_ctr_freq / 1000000;
344		pmct->pmct_value = rdtsc();
345		rv = 0;
346		break;
347
348	default:
349		rv = ENOTTY;
350	}
351
352	return rv;
353}
354