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