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