]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/perfmon.c
Merge llvm-project release/16.x llvmorg-16.0.1-0-gcd89023f7979
[FreeBSD/FreeBSD.git] / sys / i386 / 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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/conf.h>
36 #include <sys/fcntl.h>
37 #include <sys/kernel.h>
38
39 #ifndef SMP
40 #include <machine/cputypes.h>
41 #endif
42 #include <machine/clock.h>
43 #include <machine/perfmon.h>
44 #include <machine/specialreg.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 /*
65  * XXX perfmon_init_dev(void *) is a split from the perfmon_init() function.
66  * This solves a problem for DEVFS users.  It loads the "perfmon" driver after
67  * the DEVFS subsystem has been kicked into action.  The SI_ORDER_ANY is to
68  * assure that it is the most lowest priority task which, guarantees the
69  * above.
70  */
71 static void perfmon_init_dev(void *);
72 SYSINIT(cpu, SI_SUB_DRIVERS, SI_ORDER_ANY, perfmon_init_dev, NULL);
73
74 static struct cdevsw perfmon_cdevsw = {
75         .d_version =    D_VERSION,
76         .d_flags =      D_NEEDGIANT,
77         .d_open =       perfmon_open,
78         .d_close =      perfmon_close,
79         .d_ioctl =      perfmon_ioctl,
80         .d_name =       "perfmon",
81 };
82
83 /*
84  * Must be called after cpu_class is set up.
85  */
86 void
87 perfmon_init(void)
88 {
89 #ifndef SMP
90         switch(cpu_class) {
91         case CPUCLASS_586:
92                 perfmon_cpuok = 1;
93                 msr_ctl[0] = MSR_P5_CESR;
94                 msr_ctl[1] = MSR_P5_CESR;
95                 msr_pmc[0] = MSR_P5_CTR0;
96                 msr_pmc[1] = MSR_P5_CTR1;
97                 writectl = writectl5;
98                 break;
99         case CPUCLASS_686:
100                 perfmon_cpuok = 1;
101                 msr_ctl[0] = MSR_EVNTSEL0;
102                 msr_ctl[1] = MSR_EVNTSEL1;
103                 msr_pmc[0] = MSR_PERFCTR0;
104                 msr_pmc[1] = MSR_PERFCTR1;
105                 writectl = writectl6;
106                 break;
107
108         default:
109                 perfmon_cpuok = 0;
110                 break;
111         }
112 #endif /* SMP */
113 }
114
115 static void
116 perfmon_init_dev(void *dummy)
117 {
118         make_dev(&perfmon_cdevsw, 32, UID_ROOT, GID_KMEM, 0640, "perfmon");
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         register_t      saveintr;
131
132         if (pmc < 0 || pmc >= NPMC)
133                 return EINVAL;
134
135         perfmon_inuse |= (1 << pmc);
136         control &= ~(PMCF_SYS_FLAGS << 16);
137         saveintr = intr_disable();
138         ctl_shadow[pmc] = control;
139         writectl(pmc);
140         wrmsr(msr_pmc[pmc], pmc_shadow[pmc] = 0);
141         intr_restore(saveintr);
142         return 0;
143 }
144
145 int
146 perfmon_get(int pmc, unsigned int *control)
147 {
148         if (pmc < 0 || pmc >= NPMC)
149                 return EINVAL;
150
151         if (perfmon_inuse & (1 << pmc)) {
152                 *control = ctl_shadow[pmc];
153                 return 0;
154         }
155         return EBUSY;           /* XXX reversed sense */
156 }
157
158 int
159 perfmon_fini(int pmc)
160 {
161         if (pmc < 0 || pmc >= NPMC)
162                 return EINVAL;
163
164         if (perfmon_inuse & (1 << pmc)) {
165                 perfmon_stop(pmc);
166                 ctl_shadow[pmc] = 0;
167                 perfmon_inuse &= ~(1 << pmc);
168                 return 0;
169         }
170         return EBUSY;           /* XXX reversed sense */
171 }
172
173 int
174 perfmon_start(int pmc)
175 {
176         register_t      saveintr;
177
178         if (pmc < 0 || pmc >= NPMC)
179                 return EINVAL;
180
181         if (perfmon_inuse & (1 << pmc)) {
182                 saveintr = intr_disable();
183                 ctl_shadow[pmc] |= (PMCF_EN << 16);
184                 wrmsr(msr_pmc[pmc], pmc_shadow[pmc]);
185                 writectl(pmc);
186                 intr_restore(saveintr);
187                 return 0;
188         }
189         return EBUSY;
190 }
191
192 int
193 perfmon_stop(int pmc)
194 {
195         register_t      saveintr;
196
197         if (pmc < 0 || pmc >= NPMC)
198                 return EINVAL;
199
200         if (perfmon_inuse & (1 << pmc)) {
201                 saveintr = intr_disable();
202                 pmc_shadow[pmc] = rdmsr(msr_pmc[pmc]) & 0xffffffffffULL;
203                 ctl_shadow[pmc] &= ~(PMCF_EN << 16);
204                 writectl(pmc);
205                 intr_restore(saveintr);
206                 return 0;
207         }
208         return EBUSY;
209 }
210
211 int
212 perfmon_read(int pmc, quad_t *val)
213 {
214         if (pmc < 0 || pmc >= NPMC)
215                 return EINVAL;
216
217         if (perfmon_inuse & (1 << pmc)) {
218                 if (ctl_shadow[pmc] & (PMCF_EN << 16))
219                         *val = rdmsr(msr_pmc[pmc]) & 0xffffffffffULL;
220                 else
221                         *val = pmc_shadow[pmc];
222                 return 0;
223         }
224
225         return EBUSY;
226 }
227
228 int
229 perfmon_reset(int pmc)
230 {
231         if (pmc < 0 || pmc >= NPMC)
232                 return EINVAL;
233
234         if (perfmon_inuse & (1 << pmc)) {
235                 wrmsr(msr_pmc[pmc], pmc_shadow[pmc] = 0);
236                 return 0;
237         }
238         return EBUSY;
239 }
240
241 #ifndef SMP
242 /*
243  * Unfortunately, the performance-monitoring registers are laid out
244  * differently in the P5 and P6.  We keep everything in P6 format
245  * internally (except for the event code), and convert to P5
246  * format as needed on those CPUs.  The writectl function pointer
247  * is set up to point to one of these functions by perfmon_init().
248  */
249 int
250 writectl6(int pmc)
251 {
252         if (pmc > 0 && !(ctl_shadow[pmc] & (PMCF_EN << 16))) {
253                 wrmsr(msr_ctl[pmc], 0);
254         } else {
255                 wrmsr(msr_ctl[pmc], ctl_shadow[pmc]);
256         }
257         return 0;
258 }
259
260 #define P5FLAG_P        0x200
261 #define P5FLAG_E        0x100
262 #define P5FLAG_USR      0x80
263 #define P5FLAG_OS       0x40
264
265 int
266 writectl5(int pmc)
267 {
268         quad_t newval = 0;
269
270         if (ctl_shadow[1] & (PMCF_EN << 16)) {
271                 if (ctl_shadow[1] & (PMCF_USR << 16))
272                         newval |= P5FLAG_USR << 16;
273                 if (ctl_shadow[1] & (PMCF_OS << 16))
274                         newval |= P5FLAG_OS << 16;
275                 if (!(ctl_shadow[1] & (PMCF_E << 16)))
276                         newval |= P5FLAG_E << 16;
277                 newval |= (ctl_shadow[1] & 0x3f) << 16;
278         }
279         if (ctl_shadow[0] & (PMCF_EN << 16)) {
280                 if (ctl_shadow[0] & (PMCF_USR << 16))
281                         newval |= P5FLAG_USR;
282                 if (ctl_shadow[0] & (PMCF_OS << 16))
283                         newval |= P5FLAG_OS;
284                 if (!(ctl_shadow[0] & (PMCF_E << 16)))
285                         newval |= P5FLAG_E;
286                 newval |= ctl_shadow[0] & 0x3f;
287         }
288
289         wrmsr(msr_ctl[0], newval);
290         return 0;               /* XXX should check for unimplemented bits */
291 }
292 #endif /* !SMP */
293
294 /*
295  * Now the user-mode interface, called from a subdevice of mem.c.
296  */
297 static int writer;
298 static int writerpmc;
299
300 static int
301 perfmon_open(struct cdev *dev, int flags, int fmt, struct thread *td)
302 {
303         if (!perfmon_cpuok)
304                 return ENXIO;
305
306         if (flags & FWRITE) {
307                 if (writer) {
308                         return EBUSY;
309                 } else {
310                         writer = 1;
311                         writerpmc = 0;
312                 }
313         }
314         return 0;
315 }
316
317 static int
318 perfmon_close(struct cdev *dev, int flags, int fmt, struct thread *td)
319 {
320         if (flags & FWRITE) {
321                 int i;
322
323                 for (i = 0; i < NPMC; i++) {
324                         if (writerpmc & (1 << i))
325                                 perfmon_fini(i);
326                 }
327                 writer = 0;
328         }
329         return 0;
330 }
331
332 static int
333 perfmon_ioctl(struct cdev *dev, u_long cmd, caddr_t param, int flags, struct thread *td)
334 {
335         struct pmc *pmc;
336         struct pmc_data *pmcd;
337         struct pmc_tstamp *pmct;
338         uint64_t freq;
339         int *ip;
340         int rv;
341
342         switch(cmd) {
343         case PMIOSETUP:
344                 if (!(flags & FWRITE))
345                         return EPERM;
346                 pmc = (struct pmc *)param;
347
348                 rv = perfmon_setup(pmc->pmc_num, pmc->pmc_val);
349                 if (!rv) {
350                         writerpmc |= (1 << pmc->pmc_num);
351                 }
352                 break;
353
354         case PMIOGET:
355                 pmc = (struct pmc *)param;
356                 rv = perfmon_get(pmc->pmc_num, &pmc->pmc_val);
357                 break;
358
359         case PMIOSTART:
360                 if (!(flags & FWRITE))
361                         return EPERM;
362
363                 ip = (int *)param;
364                 rv = perfmon_start(*ip);
365                 break;
366
367         case PMIOSTOP:
368                 if (!(flags & FWRITE))
369                         return EPERM;
370
371                 ip = (int *)param;
372                 rv = perfmon_stop(*ip);
373                 break;
374
375         case PMIORESET:
376                 if (!(flags & FWRITE))
377                         return EPERM;
378
379                 ip = (int *)param;
380                 rv = perfmon_reset(*ip);
381                 break;
382
383         case PMIOREAD:
384                 pmcd = (struct pmc_data *)param;
385                 rv = perfmon_read(pmcd->pmcd_num, &pmcd->pmcd_value);
386                 break;
387
388         case PMIOTSTAMP:
389                 freq = atomic_load_acq_64(&tsc_freq);
390                 if (freq == 0) {
391                         rv = ENOTTY;
392                         break;
393                 }
394                 pmct = (struct pmc_tstamp *)param;
395                 /* XXX interface loses precision. */
396                 pmct->pmct_rate = freq / 1000000;
397                 pmct->pmct_value = rdtsc();
398                 rv = 0;
399                 break;
400         default:
401                 rv = ENOTTY;
402         }
403
404         return rv;
405 }