xref: /freebsd/sys/dev/hwpmc/hwpmc_tsc.c (revision 04dacd5691fb3de5fddd0868836c5a7ff9888370)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008 Joseph Koshy
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/pmc.h>
34 #include <sys/pmckern.h>
35 #include <sys/systm.h>
36 
37 #include <machine/specialreg.h>
38 
39 /*
40  * TSC support.
41  */
42 
43 #define	TSC_CAPS	PMC_CAP_READ
44 
45 struct tsc_descr {
46 	struct pmc_descr pm_descr;  /* "base class" */
47 };
48 
49 static struct tsc_descr tsc_pmcdesc[TSC_NPMCS] =
50 {
51     {
52 	.pm_descr =
53 	{
54 		.pd_name  = "TSC",
55 		.pd_class = PMC_CLASS_TSC,
56 		.pd_caps  = TSC_CAPS,
57 		.pd_width = 64
58 	}
59     }
60 };
61 
62 /*
63  * Per-CPU data structure for TSCs.
64  */
65 
66 struct tsc_cpu {
67 	struct pmc_hw	tc_hw;
68 };
69 
70 static struct tsc_cpu **tsc_pcpu;
71 
72 static int
73 tsc_allocate_pmc(int cpu __diagused, int ri __diagused, struct pmc *pm __unused,
74     const struct pmc_op_pmcallocate *a)
75 {
76 
77 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
78 	    ("[tsc,%d] illegal CPU value %d", __LINE__, cpu));
79 	KASSERT(ri >= 0 && ri < TSC_NPMCS,
80 	    ("[tsc,%d] illegal row index %d", __LINE__, ri));
81 
82 	if (a->pm_class != PMC_CLASS_TSC)
83 		return (EINVAL);
84 
85 	if (a->pm_ev != PMC_EV_TSC_TSC ||
86 	    a->pm_mode != PMC_MODE_SC)
87 		return (EINVAL);
88 
89 	return (0);
90 }
91 
92 static int
93 tsc_config_pmc(int cpu, int ri, struct pmc *pm)
94 {
95 	struct pmc_hw *phw;
96 
97 	PMCDBG3(MDP,CFG,1, "cpu=%d ri=%d pm=%p", cpu, ri, pm);
98 
99 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
100 	    ("[tsc,%d] illegal CPU value %d", __LINE__, cpu));
101 	KASSERT(ri == 0, ("[tsc,%d] illegal row-index %d", __LINE__, ri));
102 
103 	phw = &tsc_pcpu[cpu]->tc_hw;
104 
105 	KASSERT(pm == NULL || phw->phw_pmc == NULL,
106 	    ("[tsc,%d] pm=%p phw->pm=%p hwpmc not unconfigured", __LINE__,
107 	    pm, phw->phw_pmc));
108 
109 	phw->phw_pmc = pm;
110 
111 	return (0);
112 }
113 
114 static int
115 tsc_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
116 {
117 	const struct tsc_descr *pd;
118 	struct pmc_hw *phw;
119 
120 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
121 	    ("[tsc,%d] illegal CPU %d", __LINE__, cpu));
122 	KASSERT(ri == 0, ("[tsc,%d] illegal row-index %d", __LINE__, ri));
123 
124 	phw = &tsc_pcpu[cpu]->tc_hw;
125 	pd  = &tsc_pmcdesc[ri];
126 
127 	strlcpy(pi->pm_name, pd->pm_descr.pd_name, sizeof(pi->pm_name));
128 	pi->pm_class = pd->pm_descr.pd_class;
129 
130 	if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
131 		pi->pm_enabled = TRUE;
132 		*ppmc          = phw->phw_pmc;
133 	} else {
134 		pi->pm_enabled = FALSE;
135 		*ppmc          = NULL;
136 	}
137 
138 	return (0);
139 }
140 
141 static int
142 tsc_get_config(int cpu, int ri __diagused, struct pmc **ppm)
143 {
144 
145 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
146 	    ("[tsc,%d] illegal CPU %d", __LINE__, cpu));
147 	KASSERT(ri == 0, ("[tsc,%d] illegal row-index %d", __LINE__, ri));
148 
149 	*ppm = tsc_pcpu[cpu]->tc_hw.phw_pmc;
150 
151 	return (0);
152 }
153 
154 static int
155 tsc_get_msr(int ri __diagused, uint32_t *msr)
156 {
157 
158 	KASSERT(ri >= 0 && ri < TSC_NPMCS,
159 	    ("[tsc,%d] ri %d out of range", __LINE__, ri));
160 
161 	*msr = MSR_TSC;
162 
163 	return (0);
164 }
165 
166 static int
167 tsc_pcpu_fini(struct pmc_mdep *md, int cpu)
168 {
169 	int ri;
170 	struct pmc_cpu *pc;
171 
172 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
173 	    ("[tsc,%d] illegal cpu %d", __LINE__, cpu));
174 	KASSERT(tsc_pcpu[cpu] != NULL, ("[tsc,%d] null pcpu", __LINE__));
175 
176 	free(tsc_pcpu[cpu], M_PMC);
177 	tsc_pcpu[cpu] = NULL;
178 
179 	ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_TSC].pcd_ri;
180 
181 	pc = pmc_pcpu[cpu];
182 	pc->pc_hwpmcs[ri] = NULL;
183 
184 	return (0);
185 }
186 
187 static int
188 tsc_pcpu_init(struct pmc_mdep *md, int cpu)
189 {
190 	int ri;
191 	struct pmc_cpu *pc;
192 	struct tsc_cpu *tsc_pc;
193 
194 
195 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
196 	    ("[tsc,%d] illegal cpu %d", __LINE__, cpu));
197 	KASSERT(tsc_pcpu, ("[tsc,%d] null pcpu", __LINE__));
198 	KASSERT(tsc_pcpu[cpu] == NULL, ("[tsc,%d] non-null per-cpu",
199 	    __LINE__));
200 
201 	tsc_pc = malloc(sizeof(struct tsc_cpu), M_PMC, M_WAITOK|M_ZERO);
202 
203 	tsc_pc->tc_hw.phw_state = PMC_PHW_FLAG_IS_ENABLED |
204 	    PMC_PHW_CPU_TO_STATE(cpu) | PMC_PHW_INDEX_TO_STATE(0) |
205 	    PMC_PHW_FLAG_IS_SHAREABLE;
206 
207 	tsc_pcpu[cpu] = tsc_pc;
208 
209 	ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_TSC].pcd_ri;
210 
211 	KASSERT(pmc_pcpu, ("[tsc,%d] null generic pcpu", __LINE__));
212 
213 	pc = pmc_pcpu[cpu];
214 
215 	KASSERT(pc, ("[tsc,%d] null generic per-cpu", __LINE__));
216 
217 	pc->pc_hwpmcs[ri] = &tsc_pc->tc_hw;
218 
219 	return (0);
220 }
221 
222 static int
223 tsc_read_pmc(int cpu, int ri, struct pmc *pm, pmc_value_t *v)
224 {
225 	enum pmc_mode mode __diagused;
226 
227 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
228 	    ("[tsc,%d] illegal CPU value %d", __LINE__, cpu));
229 	KASSERT(ri == 0, ("[tsc,%d] illegal ri %d", __LINE__, ri));
230 
231 	mode = PMC_TO_MODE(pm);
232 
233 	KASSERT(mode == PMC_MODE_SC,
234 	    ("[tsc,%d] illegal pmc mode %d", __LINE__, mode));
235 
236 	PMCDBG1(MDP,REA,1,"tsc-read id=%d", ri);
237 
238 	*v = rdtsc();
239 
240 	return (0);
241 }
242 
243 static int
244 tsc_release_pmc(int cpu, int ri __diagused, struct pmc *pmc __unused)
245 {
246 	struct pmc_hw *phw __diagused;
247 
248 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
249 	    ("[tsc,%d] illegal CPU value %d", __LINE__, cpu));
250 	KASSERT(ri == 0,
251 	    ("[tsc,%d] illegal row-index %d", __LINE__, ri));
252 
253 	phw = &tsc_pcpu[cpu]->tc_hw;
254 
255 	KASSERT(phw->phw_pmc == NULL,
256 	    ("[tsc,%d] PHW pmc %p non-NULL", __LINE__, phw->phw_pmc));
257 
258 	/*
259 	 * Nothing to do.
260 	 */
261 	return (0);
262 }
263 
264 static int
265 tsc_start_pmc(int cpu __diagused, int ri __diagused, struct pmc *pm __unused)
266 {
267 
268 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
269 	    ("[tsc,%d] illegal CPU value %d", __LINE__, cpu));
270 	KASSERT(ri == 0, ("[tsc,%d] illegal row-index %d", __LINE__, ri));
271 
272 	return (0);	/* TSCs are always running. */
273 }
274 
275 static int
276 tsc_stop_pmc(int cpu __diagused, int ri __diagused, struct pmc *pm __unused)
277 {
278 
279 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
280 	    ("[tsc,%d] illegal CPU value %d", __LINE__, cpu));
281 	KASSERT(ri == 0, ("[tsc,%d] illegal row-index %d", __LINE__, ri));
282 
283 	return (0);	/* Cannot actually stop a TSC. */
284 }
285 
286 static int
287 tsc_write_pmc(int cpu __diagused, int ri __diagused, struct pmc *pm __unused,
288     pmc_value_t v __unused)
289 {
290 
291 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
292 	    ("[tsc,%d] illegal CPU value %d", __LINE__, cpu));
293 	KASSERT(ri == 0, ("[tsc,%d] illegal row-index %d", __LINE__, ri));
294 
295 	/*
296 	 * The TSCs are used as timecounters by the kernel, so even
297 	 * though some i386 CPUs support writeable TSCs, we don't
298 	 * support writing changing TSC values through the HWPMC API.
299 	 */
300 	return (0);
301 }
302 
303 int
304 pmc_tsc_initialize(struct pmc_mdep *md, int maxcpu)
305 {
306 	struct pmc_classdep *pcd;
307 
308 	KASSERT(md != NULL, ("[tsc,%d] md is NULL", __LINE__));
309 	KASSERT(md->pmd_nclass >= 1, ("[tsc,%d] dubious md->nclass %d",
310 	    __LINE__, md->pmd_nclass));
311 
312 	tsc_pcpu = malloc(sizeof(struct tsc_cpu *) * maxcpu, M_PMC,
313 	    M_ZERO|M_WAITOK);
314 
315 	pcd = &md->pmd_classdep[PMC_MDEP_CLASS_INDEX_TSC];
316 
317 	pcd->pcd_caps	= PMC_CAP_READ;
318 	pcd->pcd_class	= PMC_CLASS_TSC;
319 	pcd->pcd_num	= TSC_NPMCS;
320 	pcd->pcd_ri	= md->pmd_npmc;
321 	pcd->pcd_width	= 64;
322 
323 	pcd->pcd_allocate_pmc = tsc_allocate_pmc;
324 	pcd->pcd_config_pmc   = tsc_config_pmc;
325 	pcd->pcd_describe     = tsc_describe;
326 	pcd->pcd_get_config   = tsc_get_config;
327 	pcd->pcd_get_msr      = tsc_get_msr;
328 	pcd->pcd_pcpu_init    = tsc_pcpu_init;
329 	pcd->pcd_pcpu_fini    = tsc_pcpu_fini;
330 	pcd->pcd_read_pmc     = tsc_read_pmc;
331 	pcd->pcd_release_pmc  = tsc_release_pmc;
332 	pcd->pcd_start_pmc    = tsc_start_pmc;
333 	pcd->pcd_stop_pmc     = tsc_stop_pmc;
334 	pcd->pcd_write_pmc    = tsc_write_pmc;
335 
336 	md->pmd_npmc += TSC_NPMCS;
337 
338 	return (0);
339 }
340 
341 void
342 pmc_tsc_finalize(struct pmc_mdep *md __diagused)
343 {
344 #ifdef	INVARIANTS
345 	int i, ncpus;
346 
347 	ncpus = pmc_cpu_max();
348 	for (i = 0; i < ncpus; i++)
349 		KASSERT(tsc_pcpu[i] == NULL, ("[tsc,%d] non-null pcpu cpu %d",
350 		    __LINE__, i));
351 
352 	KASSERT(md->pmd_classdep[PMC_MDEP_CLASS_INDEX_TSC].pcd_class ==
353 	    PMC_CLASS_TSC, ("[tsc,%d] class mismatch", __LINE__));
354 #endif
355 
356 	free(tsc_pcpu, M_PMC);
357 	tsc_pcpu = NULL;
358 }
359