-WARNS6 cleanup (1556 warnings)
[dragonfly.git] / sys / platform / pc32 / i386 / perfmon.c
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  * $FreeBSD: src/sys/i386/i386/perfmon.c,v 1.21 1999/09/25 18:24:04 phk Exp $
30  * $DragonFly: src/sys/platform/pc32/i386/perfmon.c,v 1.9 2006/07/28 02:17:39 dillon Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/conf.h>
36 #include <sys/device.h>
37 #include <sys/fcntl.h>
38 #include <sys/lock.h>
39
40 #ifndef SMP
41 #include <machine/cputypes.h>
42 #endif
43 #include <machine/clock.h>
44 #include <machine/perfmon.h>
45
46 static int perfmon_inuse;
47 static int perfmon_cpuok;
48 #ifndef SMP
49 static int msr_ctl[NPMC];
50 #endif
51 static int msr_pmc[NPMC];
52 static unsigned int ctl_shadow[NPMC];
53 static quad_t pmc_shadow[NPMC]; /* used when ctr is stopped on P5 */
54 static int (*writectl)(int);
55 #ifndef SMP
56 static int writectl5(int);
57 static int writectl6(int);
58 #endif
59
60 static d_close_t        perfmon_close;
61 static d_open_t         perfmon_open;
62 static d_ioctl_t        perfmon_ioctl;
63
64 #define CDEV_MAJOR 2    /* We're really a minor of mem.c */
65 static struct dev_ops perfmon_ops = {
66         { "perfmon", CDEV_MAJOR, 0 },
67         .d_open =       perfmon_open,
68         .d_close =      perfmon_close,
69         .d_ioctl =      perfmon_ioctl,
70 };
71
72 /*
73  * Must be called after cpu_class is set up.
74  */
75 void
76 perfmon_init(void)
77 {
78 #ifndef SMP
79         switch(cpu_class) {
80         case CPUCLASS_586:
81                 perfmon_cpuok = 1;
82                 msr_ctl[0] = 0x11;
83                 msr_ctl[1] = 0x11;
84                 msr_pmc[0] = 0x12;
85                 msr_pmc[1] = 0x13;
86                 writectl = writectl5;
87                 break;
88         case CPUCLASS_686:
89                 perfmon_cpuok = 1;
90                 msr_ctl[0] = 0x186;
91                 msr_ctl[1] = 0x187;
92                 msr_pmc[0] = 0xc1;
93                 msr_pmc[1] = 0xc2;
94                 writectl = writectl6;
95                 break;
96
97         default:
98                 perfmon_cpuok = 0;
99                 break;
100         }
101 #endif /* SMP */
102
103         /* NOTE: really a minor of mem.  perfmon gets 32-47 */
104         dev_ops_add(&perfmon_ops, 0xf0, 32);
105         make_dev(&perfmon_ops, 32, UID_ROOT, GID_KMEM, 0640, "perfmon");
106 }
107
108 int
109 perfmon_avail(void)
110 {
111         return perfmon_cpuok;
112 }
113
114 int
115 perfmon_setup(int pmc, unsigned int control)
116 {
117         if (pmc < 0 || pmc >= NPMC)
118                 return EINVAL;
119
120         perfmon_inuse |= (1 << pmc);
121         control &= ~(PMCF_SYS_FLAGS << 16);
122         mpintr_lock();  /* doesn't have to be mpintr_lock YYY */
123         ctl_shadow[pmc] = control;
124         writectl(pmc);
125         wrmsr(msr_pmc[pmc], pmc_shadow[pmc] = 0);
126         mpintr_unlock();
127         return 0;
128 }
129
130 int
131 perfmon_get(int pmc, unsigned int *control)
132 {
133         if (pmc < 0 || pmc >= NPMC)
134                 return EINVAL;
135
136         if (perfmon_inuse & (1 << pmc)) {
137                 *control = ctl_shadow[pmc];
138                 return 0;
139         }
140         return EBUSY;           /* XXX reversed sense */
141 }
142
143 int
144 perfmon_fini(int pmc)
145 {
146         if (pmc < 0 || pmc >= NPMC)
147                 return EINVAL;
148
149         if (perfmon_inuse & (1 << pmc)) {
150                 perfmon_stop(pmc);
151                 ctl_shadow[pmc] = 0;
152                 perfmon_inuse &= ~(1 << pmc);
153                 return 0;
154         }
155         return EBUSY;           /* XXX reversed sense */
156 }
157
158 int
159 perfmon_start(int pmc)
160 {
161         if (pmc < 0 || pmc >= NPMC)
162                 return EINVAL;
163
164         if (perfmon_inuse & (1 << pmc)) {
165                 mpintr_lock();  /* doesn't have to be mpintr YYY */
166                 ctl_shadow[pmc] |= (PMCF_EN << 16);
167                 wrmsr(msr_pmc[pmc], pmc_shadow[pmc]);
168                 writectl(pmc);
169                 mpintr_unlock();
170                 return 0;
171         }
172         return EBUSY;
173 }
174
175 int
176 perfmon_stop(int pmc)
177 {
178         if (pmc < 0 || pmc >= NPMC)
179                 return EINVAL;
180
181         if (perfmon_inuse & (1 << pmc)) {
182                 mpintr_lock();
183                 pmc_shadow[pmc] = rdmsr(msr_pmc[pmc]) & 0xffffffffffULL;
184                 ctl_shadow[pmc] &= ~(PMCF_EN << 16);
185                 writectl(pmc);
186                 mpintr_unlock();
187                 return 0;
188         }
189         return EBUSY;
190 }
191
192 int
193 perfmon_read(int pmc, quad_t *val)
194 {
195         if (pmc < 0 || pmc >= NPMC)
196                 return EINVAL;
197
198         if (perfmon_inuse & (1 << pmc)) {
199                 if (ctl_shadow[pmc] & (PMCF_EN << 16))
200                         *val = rdmsr(msr_pmc[pmc]) & 0xffffffffffULL;
201                 else
202                         *val = pmc_shadow[pmc];
203                 return 0;
204         }
205
206         return EBUSY;
207 }
208
209 int
210 perfmon_reset(int pmc)
211 {
212         if (pmc < 0 || pmc >= NPMC)
213                 return EINVAL;
214
215         if (perfmon_inuse & (1 << pmc)) {
216                 wrmsr(msr_pmc[pmc], pmc_shadow[pmc] = 0);
217                 return 0;
218         }
219         return EBUSY;
220 }
221
222 #ifndef SMP
223 /*
224  * Unfortunately, the performance-monitoring registers are laid out
225  * differently in the P5 and P6.  We keep everything in P6 format
226  * internally (except for the event code), and convert to P5
227  * format as needed on those CPUs.  The writectl function pointer
228  * is set up to point to one of these functions by perfmon_init().
229  */
230 int
231 writectl6(int pmc)
232 {
233         if (pmc > 0 && !(ctl_shadow[pmc] & (PMCF_EN << 16))) {
234                 wrmsr(msr_ctl[pmc], 0);
235         } else {
236                 wrmsr(msr_ctl[pmc], ctl_shadow[pmc]);
237         }
238         return 0;
239 }
240
241 #define P5FLAG_P        0x200
242 #define P5FLAG_E        0x100
243 #define P5FLAG_USR      0x80
244 #define P5FLAG_OS       0x40
245
246 int
247 writectl5(int pmc)
248 {
249         quad_t newval = 0;
250
251         if (ctl_shadow[1] & (PMCF_EN << 16)) {
252                 if (ctl_shadow[1] & (PMCF_USR << 16))
253                         newval |= P5FLAG_USR << 16;
254                 if (ctl_shadow[1] & (PMCF_OS << 16))
255                         newval |= P5FLAG_OS << 16;
256                 if (!(ctl_shadow[1] & (PMCF_E << 16)))
257                         newval |= P5FLAG_E << 16;
258                 newval |= (ctl_shadow[1] & 0x3f) << 16;
259         }
260         if (ctl_shadow[0] & (PMCF_EN << 16)) {
261                 if (ctl_shadow[0] & (PMCF_USR << 16))
262                         newval |= P5FLAG_USR;
263                 if (ctl_shadow[0] & (PMCF_OS << 16))
264                         newval |= P5FLAG_OS;
265                 if (!(ctl_shadow[0] & (PMCF_E << 16)))
266                         newval |= P5FLAG_E;
267                 newval |= ctl_shadow[0] & 0x3f;
268         }
269
270         wrmsr(msr_ctl[0], newval);
271         return 0;               /* XXX should check for unimplemented bits */
272 }
273 #endif /* !SMP */
274
275 /*
276  * Now the user-mode interface, called from a subdevice of mem.c.
277  */
278 static int writer;
279 static int writerpmc;
280
281 static int
282 perfmon_open(struct dev_open_args *ap)
283 {
284         if (!perfmon_cpuok)
285                 return ENXIO;
286
287         if (ap->a_oflags & FWRITE) {
288                 if (writer) {
289                         return EBUSY;
290                 } else {
291                         writer = 1;
292                         writerpmc = 0;
293                 }
294         }
295         return 0;
296 }
297
298 static int
299 perfmon_close(struct dev_close_args *ap)
300 {
301         if (ap->a_fflag & FWRITE) {
302                 int i;
303
304                 for (i = 0; i < NPMC; i++) {
305                         if (writerpmc & (1 << i))
306                                 perfmon_fini(i);
307                 }
308                 writer = 0;
309         }
310         return 0;
311 }
312
313 static int
314 perfmon_ioctl(struct dev_ioctl_args *ap)
315 {
316         caddr_t param = ap->a_data;
317         struct pmc *pmc;
318         struct pmc_data *pmcd;
319         struct pmc_tstamp *pmct;
320         int *ip;
321         int rv;
322
323         switch(ap->a_cmd) {
324         case PMIOSETUP:
325                 if (!(ap->a_fflag & FWRITE))
326                         return EPERM;
327                 pmc = (struct pmc *)param;
328
329                 rv = perfmon_setup(pmc->pmc_num, pmc->pmc_val);
330                 if (!rv) {
331                         writerpmc |= (1 << pmc->pmc_num);
332                 }
333                 break;
334
335         case PMIOGET:
336                 pmc = (struct pmc *)param;
337                 rv = perfmon_get(pmc->pmc_num, &pmc->pmc_val);
338                 break;
339
340         case PMIOSTART:
341                 if (!(ap->a_fflag & FWRITE))
342                         return EPERM;
343
344                 ip = (int *)param;
345                 rv = perfmon_start(*ip);
346                 break;
347
348         case PMIOSTOP:
349                 if (!(ap->a_fflag & FWRITE))
350                         return EPERM;
351
352                 ip = (int *)param;
353                 rv = perfmon_stop(*ip);
354                 break;
355
356         case PMIORESET:
357                 if (!(ap->a_fflag & FWRITE))
358                         return EPERM;
359
360                 ip = (int *)param;
361                 rv = perfmon_reset(*ip);
362                 break;
363
364         case PMIOREAD:
365                 pmcd = (struct pmc_data *)param;
366                 rv = perfmon_read(pmcd->pmcd_num, &pmcd->pmcd_value);
367                 break;
368
369         case PMIOTSTAMP:
370                 if (!tsc_freq) {
371                         rv = ENOTTY;
372                         break;
373                 }
374                 pmct = (struct pmc_tstamp *)param;
375                 /* XXX interface loses precision. */
376                 pmct->pmct_rate = tsc_freq / 1000000;
377                 pmct->pmct_value = rdtsc();
378                 rv = 0;
379                 break;
380         default:
381                 rv = ENOTTY;
382         }
383
384         return rv;
385 }