2 * Copyright 1996 Massachusetts Institute of Technology
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
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
29 * $FreeBSD: src/sys/i386/i386/perfmon.c,v 1.21 1999/09/25 18:24:04 phk Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/device.h>
37 #include <sys/fcntl.h>
40 #include <machine/clock.h>
41 #include <machine/perfmon.h>
43 static int perfmon_inuse;
44 static int perfmon_cpuok;
45 static int msr_pmc[NPMC];
46 static unsigned int ctl_shadow[NPMC];
47 static quad_t pmc_shadow[NPMC]; /* used when ctr is stopped on P5 */
48 static int (*writectl)(int);
50 static d_close_t perfmon_close;
51 static d_open_t perfmon_open;
52 static d_ioctl_t perfmon_ioctl;
54 static struct dev_ops perfmon_ops = {
56 .d_open = perfmon_open,
57 .d_close = perfmon_close,
58 .d_ioctl = perfmon_ioctl,
62 * Initialize the device ops for user access to the perfmon. This must
63 * be done late in the boot sequence.
65 * NOTE: The perfmon is really a minor of the mem major. Perfmon
69 perfmon_driver_init(void *unused __unused)
71 make_dev(&perfmon_ops, 32, UID_ROOT, GID_KMEM, 0640, "perfmon");
74 SYSINIT(perfmondrv, SI_SUB_DRIVERS, SI_ORDER_ANY, perfmon_driver_init, NULL)
77 * This is called in early boot, after cpu_class has been set up.
91 perfmon_setup(int pmc, unsigned int control)
93 if (pmc < 0 || pmc >= NPMC)
96 perfmon_inuse |= (1 << pmc);
97 control &= ~(PMCF_SYS_FLAGS << 16);
98 mpintr_lock(); /* doesn't have to be mpintr_lock YYY */
99 ctl_shadow[pmc] = control;
101 wrmsr(msr_pmc[pmc], pmc_shadow[pmc] = 0);
107 perfmon_get(int pmc, unsigned int *control)
109 if (pmc < 0 || pmc >= NPMC)
112 if (perfmon_inuse & (1 << pmc)) {
113 *control = ctl_shadow[pmc];
116 return EBUSY; /* XXX reversed sense */
120 perfmon_fini(int pmc)
122 if (pmc < 0 || pmc >= NPMC)
125 if (perfmon_inuse & (1 << pmc)) {
128 perfmon_inuse &= ~(1 << pmc);
131 return EBUSY; /* XXX reversed sense */
135 perfmon_start(int pmc)
137 if (pmc < 0 || pmc >= NPMC)
140 if (perfmon_inuse & (1 << pmc)) {
141 mpintr_lock(); /* doesn't have to be mpintr YYY */
142 ctl_shadow[pmc] |= (PMCF_EN << 16);
143 wrmsr(msr_pmc[pmc], pmc_shadow[pmc]);
152 perfmon_stop(int pmc)
154 if (pmc < 0 || pmc >= NPMC)
157 if (perfmon_inuse & (1 << pmc)) {
159 pmc_shadow[pmc] = rdmsr(msr_pmc[pmc]) & 0xffffffffffULL;
160 ctl_shadow[pmc] &= ~(PMCF_EN << 16);
169 perfmon_read(int pmc, quad_t *val)
171 if (pmc < 0 || pmc >= NPMC)
174 if (perfmon_inuse & (1 << pmc)) {
175 if (ctl_shadow[pmc] & (PMCF_EN << 16))
176 *val = rdmsr(msr_pmc[pmc]) & 0xffffffffffULL;
178 *val = pmc_shadow[pmc];
186 perfmon_reset(int pmc)
188 if (pmc < 0 || pmc >= NPMC)
191 if (perfmon_inuse & (1 << pmc)) {
192 wrmsr(msr_pmc[pmc], pmc_shadow[pmc] = 0);
199 * Now the user-mode interface, called from a subdevice of mem.c.
202 static int writerpmc;
205 perfmon_open(struct dev_open_args *ap)
210 if (ap->a_oflags & FWRITE) {
222 perfmon_close(struct dev_close_args *ap)
224 if (ap->a_fflag & FWRITE) {
227 for (i = 0; i < NPMC; i++) {
228 if (writerpmc & (1 << i))
237 perfmon_ioctl(struct dev_ioctl_args *ap)
239 caddr_t param = ap->a_data;
241 struct pmc_data *pmcd;
242 struct pmc_tstamp *pmct;
248 if (!(ap->a_fflag & FWRITE))
250 pmc = (struct pmc *)param;
252 rv = perfmon_setup(pmc->pmc_num, pmc->pmc_val);
254 writerpmc |= (1 << pmc->pmc_num);
259 pmc = (struct pmc *)param;
260 rv = perfmon_get(pmc->pmc_num, &pmc->pmc_val);
264 if (!(ap->a_fflag & FWRITE))
268 rv = perfmon_start(*ip);
272 if (!(ap->a_fflag & FWRITE))
276 rv = perfmon_stop(*ip);
280 if (!(ap->a_fflag & FWRITE))
284 rv = perfmon_reset(*ip);
288 pmcd = (struct pmc_data *)param;
289 rv = perfmon_read(pmcd->pmcd_num, &pmcd->pmcd_value);
293 if (tsc_frequency == 0) {
297 pmct = (struct pmc_tstamp *)param;
298 /* XXX interface loses precision. */
299 pmct->pmct_rate = (int)(tsc_frequency / 1000000);
300 pmct->pmct_value = rdtsc();