xref: /freebsd/sys/dev/hwpmc/hwpmc_uncore.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 Fabien Thomas
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 /*
30  * Intel Uncore PMCs.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/pmc.h>
39 #include <sys/pmckern.h>
40 #include <sys/systm.h>
41 
42 #include <machine/intr_machdep.h>
43 #include <x86/apicvar.h>
44 #include <machine/cpu.h>
45 #include <machine/cpufunc.h>
46 #include <machine/specialreg.h>
47 
48 #define	UCF_PMC_CAPS \
49 	(PMC_CAP_READ | PMC_CAP_WRITE)
50 
51 #define	UCP_PMC_CAPS \
52     (PMC_CAP_EDGE | PMC_CAP_THRESHOLD | PMC_CAP_READ | PMC_CAP_WRITE | \
53     PMC_CAP_INVERT | PMC_CAP_QUALIFIER | PMC_CAP_PRECISE)
54 
55 #define	SELECTSEL(x) \
56 	(((x) == PMC_CPU_INTEL_SANDYBRIDGE || (x) == PMC_CPU_INTEL_HASWELL) ? \
57 	UCP_CB0_EVSEL0 : UCP_EVSEL0)
58 
59 #define SELECTOFF(x) \
60 	(((x) == PMC_CPU_INTEL_SANDYBRIDGE || (x) == PMC_CPU_INTEL_HASWELL) ? \
61 	UCF_OFFSET_SB : UCF_OFFSET)
62 
63 static enum pmc_cputype	uncore_cputype;
64 
65 struct uncore_cpu {
66 	volatile uint32_t	pc_ucfctrl;	/* Fixed function control. */
67 	volatile uint64_t	pc_globalctrl;	/* Global control register. */
68 	struct pmc_hw		pc_uncorepmcs[];
69 };
70 
71 static struct uncore_cpu **uncore_pcpu;
72 
73 static uint64_t uncore_pmcmask;
74 
75 static int uncore_ucf_ri;		/* relative index of fixed counters */
76 static int uncore_ucf_width;
77 static int uncore_ucf_npmc;
78 
79 static int uncore_ucp_width;
80 static int uncore_ucp_npmc;
81 
82 static int
83 uncore_pcpu_noop(struct pmc_mdep *md, int cpu)
84 {
85 	(void) md;
86 	(void) cpu;
87 	return (0);
88 }
89 
90 static int
91 uncore_pcpu_init(struct pmc_mdep *md, int cpu)
92 {
93 	struct pmc_cpu *pc;
94 	struct uncore_cpu *cc;
95 	struct pmc_hw *phw;
96 	int uncore_ri, n, npmc;
97 
98 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
99 	    ("[ucf,%d] insane cpu number %d", __LINE__, cpu));
100 
101 	PMCDBG1(MDP,INI,1,"uncore-init cpu=%d", cpu);
102 
103 	uncore_ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCP].pcd_ri;
104 	npmc = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCP].pcd_num;
105 	npmc += md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCF].pcd_num;
106 
107 	cc = malloc(sizeof(struct uncore_cpu) + npmc * sizeof(struct pmc_hw),
108 	    M_PMC, M_WAITOK | M_ZERO);
109 
110 	uncore_pcpu[cpu] = cc;
111 	pc = pmc_pcpu[cpu];
112 
113 	KASSERT(pc != NULL && cc != NULL,
114 	    ("[uncore,%d] NULL per-cpu structures cpu=%d", __LINE__, cpu));
115 
116 	for (n = 0, phw = cc->pc_uncorepmcs; n < npmc; n++, phw++) {
117 		phw->phw_state 	  = PMC_PHW_FLAG_IS_ENABLED |
118 		    PMC_PHW_CPU_TO_STATE(cpu) |
119 		    PMC_PHW_INDEX_TO_STATE(n + uncore_ri);
120 		phw->phw_pmc	  = NULL;
121 		pc->pc_hwpmcs[n + uncore_ri]  = phw;
122 	}
123 
124 	return (0);
125 }
126 
127 static int
128 uncore_pcpu_fini(struct pmc_mdep *md, int cpu)
129 {
130 	int uncore_ri, n, npmc;
131 	struct pmc_cpu *pc;
132 	struct uncore_cpu *cc;
133 
134 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
135 	    ("[uncore,%d] insane cpu number (%d)", __LINE__, cpu));
136 
137 	PMCDBG1(MDP,INI,1,"uncore-pcpu-fini cpu=%d", cpu);
138 
139 	if ((cc = uncore_pcpu[cpu]) == NULL)
140 		return (0);
141 
142 	uncore_pcpu[cpu] = NULL;
143 
144 	pc = pmc_pcpu[cpu];
145 
146 	KASSERT(pc != NULL, ("[uncore,%d] NULL per-cpu %d state", __LINE__,
147 		cpu));
148 
149 	npmc = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCP].pcd_num;
150 	uncore_ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCP].pcd_ri;
151 
152 	for (n = 0; n < npmc; n++)
153 		wrmsr(SELECTSEL(uncore_cputype) + n, 0);
154 
155 	wrmsr(UCF_CTRL, 0);
156 	npmc += md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCF].pcd_num;
157 
158 	for (n = 0; n < npmc; n++)
159 		pc->pc_hwpmcs[n + uncore_ri] = NULL;
160 
161 	free(cc, M_PMC);
162 
163 	return (0);
164 }
165 
166 /*
167  * Fixed function counters.
168  */
169 
170 static pmc_value_t
171 ucf_perfctr_value_to_reload_count(pmc_value_t v)
172 {
173 
174 	/* If the PMC has overflowed, return a reload count of zero. */
175 	if ((v & (1ULL << (uncore_ucf_width - 1))) == 0)
176 		return (0);
177 	v &= (1ULL << uncore_ucf_width) - 1;
178 	return (1ULL << uncore_ucf_width) - v;
179 }
180 
181 static pmc_value_t
182 ucf_reload_count_to_perfctr_value(pmc_value_t rlc)
183 {
184 	return (1ULL << uncore_ucf_width) - rlc;
185 }
186 
187 static int
188 ucf_allocate_pmc(int cpu, int ri, struct pmc *pm,
189     const struct pmc_op_pmcallocate *a)
190 {
191 	uint32_t flags;
192 
193 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
194 	    ("[uncore,%d] illegal CPU %d", __LINE__, cpu));
195 
196 	PMCDBG2(MDP,ALL,1, "ucf-allocate ri=%d reqcaps=0x%x", ri, pm->pm_caps);
197 
198 	if (ri < 0 || ri > uncore_ucf_npmc)
199 		return (EINVAL);
200 
201 	if (a->pm_class != PMC_CLASS_UCF)
202 		return (EINVAL);
203 
204 	flags = UCF_EN;
205 
206 	pm->pm_md.pm_ucf.pm_ucf_ctrl = (flags << (ri * 4));
207 
208 	PMCDBG1(MDP,ALL,2, "ucf-allocate config=0x%jx",
209 	    (uintmax_t) pm->pm_md.pm_ucf.pm_ucf_ctrl);
210 
211 	return (0);
212 }
213 
214 static int
215 ucf_config_pmc(int cpu, int ri, struct pmc *pm)
216 {
217 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
218 	    ("[uncore,%d] illegal CPU %d", __LINE__, cpu));
219 
220 	KASSERT(ri >= 0 && ri < uncore_ucf_npmc,
221 	    ("[uncore,%d] illegal row-index %d", __LINE__, ri));
222 
223 	PMCDBG3(MDP,CFG,1, "ucf-config cpu=%d ri=%d pm=%p", cpu, ri, pm);
224 
225 	KASSERT(uncore_pcpu[cpu] != NULL, ("[uncore,%d] null per-cpu %d", __LINE__,
226 	    cpu));
227 
228 	uncore_pcpu[cpu]->pc_uncorepmcs[ri + uncore_ucf_ri].phw_pmc = pm;
229 
230 	return (0);
231 }
232 
233 static int
234 ucf_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
235 {
236 	struct pmc_hw *phw;
237 
238 	phw = &uncore_pcpu[cpu]->pc_uncorepmcs[ri + uncore_ucf_ri];
239 
240 	snprintf(pi->pm_name, sizeof(pi->pm_name), "UCF-%d", ri);
241 	pi->pm_class = PMC_CLASS_UCF;
242 
243 	if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
244 		pi->pm_enabled = TRUE;
245 		*ppmc          = phw->phw_pmc;
246 	} else {
247 		pi->pm_enabled = FALSE;
248 		*ppmc          = NULL;
249 	}
250 
251 	return (0);
252 }
253 
254 static int
255 ucf_get_config(int cpu, int ri, struct pmc **ppm)
256 {
257 	*ppm = uncore_pcpu[cpu]->pc_uncorepmcs[ri + uncore_ucf_ri].phw_pmc;
258 
259 	return (0);
260 }
261 
262 static int
263 ucf_read_pmc(int cpu, int ri, struct pmc *pm, pmc_value_t *v)
264 {
265 	pmc_value_t tmp;
266 
267 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
268 	    ("[uncore,%d] illegal cpu value %d", __LINE__, cpu));
269 	KASSERT(ri >= 0 && ri < uncore_ucf_npmc,
270 	    ("[uncore,%d] illegal row-index %d", __LINE__, ri));
271 
272 	tmp = rdmsr(UCF_CTR0 + ri);
273 
274 	if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
275 		*v = ucf_perfctr_value_to_reload_count(tmp);
276 	else
277 		*v = tmp;
278 
279 	PMCDBG3(MDP,REA,1, "ucf-read cpu=%d ri=%d -> v=%jx", cpu, ri, *v);
280 
281 	return (0);
282 }
283 
284 static int
285 ucf_release_pmc(int cpu, int ri, struct pmc *pmc)
286 {
287 	PMCDBG3(MDP,REL,1, "ucf-release cpu=%d ri=%d pm=%p", cpu, ri, pmc);
288 
289 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
290 	    ("[uncore,%d] illegal CPU value %d", __LINE__, cpu));
291 	KASSERT(ri >= 0 && ri < uncore_ucf_npmc,
292 	    ("[uncore,%d] illegal row-index %d", __LINE__, ri));
293 
294 	KASSERT(uncore_pcpu[cpu]->pc_uncorepmcs[ri + uncore_ucf_ri].phw_pmc == NULL,
295 	    ("[uncore,%d] PHW pmc non-NULL", __LINE__));
296 
297 	return (0);
298 }
299 
300 static int
301 ucf_start_pmc(int cpu, int ri, struct pmc *pm)
302 {
303 	struct uncore_cpu *ucfc;
304 
305 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
306 	    ("[uncore,%d] illegal CPU value %d", __LINE__, cpu));
307 	KASSERT(ri >= 0 && ri < uncore_ucf_npmc,
308 	    ("[uncore,%d] illegal row-index %d", __LINE__, ri));
309 
310 	PMCDBG2(MDP,STA,1,"ucf-start cpu=%d ri=%d", cpu, ri);
311 
312 	ucfc = uncore_pcpu[cpu];
313 	ucfc->pc_ucfctrl |= pm->pm_md.pm_ucf.pm_ucf_ctrl;
314 
315 	wrmsr(UCF_CTRL, ucfc->pc_ucfctrl);
316 
317 	ucfc->pc_globalctrl |= (1ULL << (ri + SELECTOFF(uncore_cputype)));
318 	wrmsr(UC_GLOBAL_CTRL, ucfc->pc_globalctrl);
319 
320 	PMCDBG4(MDP,STA,1,"ucfctrl=%x(%x) globalctrl=%jx(%jx)",
321 	    ucfc->pc_ucfctrl, (uint32_t) rdmsr(UCF_CTRL),
322 	    ucfc->pc_globalctrl, rdmsr(UC_GLOBAL_CTRL));
323 
324 	return (0);
325 }
326 
327 static int
328 ucf_stop_pmc(int cpu, int ri, struct pmc *pm __unused)
329 {
330 	uint32_t fc;
331 	struct uncore_cpu *ucfc;
332 
333 	PMCDBG2(MDP,STO,1,"ucf-stop cpu=%d ri=%d", cpu, ri);
334 
335 	ucfc = uncore_pcpu[cpu];
336 
337 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
338 	    ("[uncore,%d] illegal CPU value %d", __LINE__, cpu));
339 	KASSERT(ri >= 0 && ri < uncore_ucf_npmc,
340 	    ("[uncore,%d] illegal row-index %d", __LINE__, ri));
341 
342 	fc = (UCF_MASK << (ri * 4));
343 
344 	ucfc->pc_ucfctrl &= ~fc;
345 
346 	PMCDBG1(MDP,STO,1,"ucf-stop ucfctrl=%x", ucfc->pc_ucfctrl);
347 	wrmsr(UCF_CTRL, ucfc->pc_ucfctrl);
348 
349 	/* Don't need to write UC_GLOBAL_CTRL, one disable is enough. */
350 
351 	PMCDBG4(MDP,STO,1,"ucfctrl=%x(%x) globalctrl=%jx(%jx)",
352 	    ucfc->pc_ucfctrl, (uint32_t) rdmsr(UCF_CTRL),
353 	    ucfc->pc_globalctrl, rdmsr(UC_GLOBAL_CTRL));
354 
355 	return (0);
356 }
357 
358 static int
359 ucf_write_pmc(int cpu, int ri, struct pmc *pm, pmc_value_t v)
360 {
361 	struct uncore_cpu *cc;
362 
363 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
364 	    ("[uncore,%d] illegal cpu value %d", __LINE__, cpu));
365 	KASSERT(ri >= 0 && ri < uncore_ucf_npmc,
366 	    ("[uncore,%d] illegal row-index %d", __LINE__, ri));
367 
368 	cc = uncore_pcpu[cpu];
369 
370 	if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
371 		v = ucf_reload_count_to_perfctr_value(v);
372 
373 	wrmsr(UCF_CTRL, 0);	/* Turn off fixed counters */
374 	wrmsr(UCF_CTR0 + ri, v);
375 	wrmsr(UCF_CTRL, cc->pc_ucfctrl);
376 
377 	PMCDBG4(MDP,WRI,1, "ucf-write cpu=%d ri=%d v=%jx ucfctrl=%jx ",
378 	    cpu, ri, v, (uintmax_t) rdmsr(UCF_CTRL));
379 
380 	return (0);
381 }
382 
383 
384 static void
385 ucf_initialize(struct pmc_mdep *md, int maxcpu, int npmc, int pmcwidth)
386 {
387 	struct pmc_classdep *pcd;
388 
389 	KASSERT(md != NULL, ("[ucf,%d] md is NULL", __LINE__));
390 
391 	PMCDBG0(MDP,INI,1, "ucf-initialize");
392 
393 	pcd = &md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCF];
394 
395 	pcd->pcd_caps	= UCF_PMC_CAPS;
396 	pcd->pcd_class	= PMC_CLASS_UCF;
397 	pcd->pcd_num	= npmc;
398 	pcd->pcd_ri	= md->pmd_npmc;
399 	pcd->pcd_width	= pmcwidth;
400 
401 	pcd->pcd_allocate_pmc	= ucf_allocate_pmc;
402 	pcd->pcd_config_pmc	= ucf_config_pmc;
403 	pcd->pcd_describe	= ucf_describe;
404 	pcd->pcd_get_config	= ucf_get_config;
405 	pcd->pcd_get_msr	= NULL;
406 	pcd->pcd_pcpu_fini	= uncore_pcpu_noop;
407 	pcd->pcd_pcpu_init	= uncore_pcpu_noop;
408 	pcd->pcd_read_pmc	= ucf_read_pmc;
409 	pcd->pcd_release_pmc	= ucf_release_pmc;
410 	pcd->pcd_start_pmc	= ucf_start_pmc;
411 	pcd->pcd_stop_pmc	= ucf_stop_pmc;
412 	pcd->pcd_write_pmc	= ucf_write_pmc;
413 
414 	md->pmd_npmc	       += npmc;
415 }
416 
417 /*
418  * Intel programmable PMCs.
419  */
420 
421 /*
422  * Event descriptor tables.
423  *
424  * For each event id, we track:
425  *
426  * 1. The CPUs that the event is valid for.
427  *
428  * 2. If the event uses a fixed UMASK, the value of the umask field.
429  *    If the event doesn't use a fixed UMASK, a mask of legal bits
430  *    to check against.
431  */
432 
433 struct ucp_event_descr {
434 	enum pmc_event	ucp_ev;
435 	unsigned char	ucp_evcode;
436 	unsigned char	ucp_umask;
437 	unsigned char	ucp_flags;
438 };
439 
440 #define	UCP_F_I7	(1 << 0)	/* CPU: Core i7 */
441 #define	UCP_F_WM	(1 << 1)	/* CPU: Westmere */
442 #define	UCP_F_SB	(1 << 2)	/* CPU: Sandy Bridge */
443 #define	UCP_F_HW	(1 << 3)	/* CPU: Haswell */
444 #define	UCP_F_FM	(1 << 4)	/* Fixed mask */
445 
446 #define	UCP_F_ALLCPUS					\
447     (UCP_F_I7 | UCP_F_WM)
448 
449 #define	UCP_F_CMASK		0xFF000000
450 
451 static pmc_value_t
452 ucp_perfctr_value_to_reload_count(pmc_value_t v)
453 {
454 	v &= (1ULL << uncore_ucp_width) - 1;
455 	return (1ULL << uncore_ucp_width) - v;
456 }
457 
458 static pmc_value_t
459 ucp_reload_count_to_perfctr_value(pmc_value_t rlc)
460 {
461 	return (1ULL << uncore_ucp_width) - rlc;
462 }
463 
464 /*
465  * Counter specific event information for Sandybridge and Haswell
466  */
467 static int
468 ucp_event_sb_hw_ok_on_counter(uint8_t ev, int ri)
469 {
470 	uint32_t mask;
471 
472 	switch (ev) {
473 		/*
474 		 * Events valid only on counter 0.
475 		 */
476 		case 0x80:
477 		case 0x83:
478 		mask = (1 << 0);
479 		break;
480 
481 	default:
482 		mask = ~0;	/* Any row index is ok. */
483 	}
484 
485 	return (mask & (1 << ri));
486 }
487 
488 static int
489 ucp_allocate_pmc(int cpu, int ri, struct pmc *pm,
490     const struct pmc_op_pmcallocate *a)
491 {
492 	uint8_t ev;
493 	const struct pmc_md_ucp_op_pmcallocate *ucp;
494 
495 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
496 	    ("[uncore,%d] illegal CPU %d", __LINE__, cpu));
497 	KASSERT(ri >= 0 && ri < uncore_ucp_npmc,
498 	    ("[uncore,%d] illegal row-index value %d", __LINE__, ri));
499 
500 	if (a->pm_class != PMC_CLASS_UCP)
501 		return (EINVAL);
502 
503 	ucp = &a->pm_md.pm_ucp;
504 	ev = UCP_EVSEL(ucp->pm_ucp_config);
505 	switch (uncore_cputype) {
506 	case PMC_CPU_INTEL_HASWELL:
507 	case PMC_CPU_INTEL_SANDYBRIDGE:
508 		if (ucp_event_sb_hw_ok_on_counter(ev, ri) == 0)
509 			return (EINVAL);
510 		break;
511 	default:
512 		break;
513 	}
514 
515 	pm->pm_md.pm_ucp.pm_ucp_evsel = ucp->pm_ucp_config | UCP_EN;
516 
517 	return (0);
518 }
519 
520 static int
521 ucp_config_pmc(int cpu, int ri, struct pmc *pm)
522 {
523 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
524 	    ("[uncore,%d] illegal CPU %d", __LINE__, cpu));
525 
526 	KASSERT(ri >= 0 && ri < uncore_ucp_npmc,
527 	    ("[uncore,%d] illegal row-index %d", __LINE__, ri));
528 
529 	PMCDBG3(MDP,CFG,1, "ucp-config cpu=%d ri=%d pm=%p", cpu, ri, pm);
530 
531 	KASSERT(uncore_pcpu[cpu] != NULL, ("[uncore,%d] null per-cpu %d", __LINE__,
532 	    cpu));
533 
534 	uncore_pcpu[cpu]->pc_uncorepmcs[ri].phw_pmc = pm;
535 
536 	return (0);
537 }
538 
539 static int
540 ucp_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
541 {
542 	struct pmc_hw *phw;
543 
544 	phw = &uncore_pcpu[cpu]->pc_uncorepmcs[ri];
545 
546 	snprintf(pi->pm_name, sizeof(pi->pm_name), "UCP-%d", ri);
547 	pi->pm_class = PMC_CLASS_UCP;
548 
549 	if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
550 		pi->pm_enabled = TRUE;
551 		*ppmc          = phw->phw_pmc;
552 	} else {
553 		pi->pm_enabled = FALSE;
554 		*ppmc          = NULL;
555 	}
556 
557 	return (0);
558 }
559 
560 static int
561 ucp_get_config(int cpu, int ri, struct pmc **ppm)
562 {
563 	*ppm = uncore_pcpu[cpu]->pc_uncorepmcs[ri].phw_pmc;
564 
565 	return (0);
566 }
567 
568 static int
569 ucp_read_pmc(int cpu, int ri, struct pmc *pm, pmc_value_t *v)
570 {
571 	pmc_value_t tmp;
572 
573 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
574 	    ("[uncore,%d] illegal cpu value %d", __LINE__, cpu));
575 	KASSERT(ri >= 0 && ri < uncore_ucp_npmc,
576 	    ("[uncore,%d] illegal row-index %d", __LINE__, ri));
577 
578 	tmp = rdmsr(UCP_PMC0 + ri);
579 	if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
580 		*v = ucp_perfctr_value_to_reload_count(tmp);
581 	else
582 		*v = tmp;
583 
584 	PMCDBG4(MDP,REA,1, "ucp-read cpu=%d ri=%d msr=0x%x -> v=%jx", cpu, ri,
585 	    ri, *v);
586 
587 	return (0);
588 }
589 
590 static int
591 ucp_release_pmc(int cpu, int ri, struct pmc *pm)
592 {
593 	(void) pm;
594 
595 	PMCDBG3(MDP,REL,1, "ucp-release cpu=%d ri=%d pm=%p", cpu, ri,
596 	    pm);
597 
598 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
599 	    ("[uncore,%d] illegal CPU value %d", __LINE__, cpu));
600 	KASSERT(ri >= 0 && ri < uncore_ucp_npmc,
601 	    ("[uncore,%d] illegal row-index %d", __LINE__, ri));
602 
603 	KASSERT(uncore_pcpu[cpu]->pc_uncorepmcs[ri].phw_pmc
604 	    == NULL, ("[uncore,%d] PHW pmc non-NULL", __LINE__));
605 
606 	return (0);
607 }
608 
609 static int
610 ucp_start_pmc(int cpu, int ri, struct pmc *pm)
611 {
612 	uint64_t evsel;
613 	struct uncore_cpu *cc;
614 
615 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
616 	    ("[uncore,%d] illegal CPU value %d", __LINE__, cpu));
617 	KASSERT(ri >= 0 && ri < uncore_ucp_npmc,
618 	    ("[uncore,%d] illegal row-index %d", __LINE__, ri));
619 
620 	cc = uncore_pcpu[cpu];
621 
622 	PMCDBG2(MDP,STA,1, "ucp-start cpu=%d ri=%d", cpu, ri);
623 
624 	evsel = pm->pm_md.pm_ucp.pm_ucp_evsel;
625 
626 	PMCDBG4(MDP,STA,2,
627 	    "ucp-start/2 cpu=%d ri=%d evselmsr=0x%x evsel=0x%x",
628 	    cpu, ri, SELECTSEL(uncore_cputype) + ri, evsel);
629 
630 	wrmsr(SELECTSEL(uncore_cputype) + ri, evsel);
631 
632 	cc->pc_globalctrl |= (1ULL << ri);
633 	wrmsr(UC_GLOBAL_CTRL, cc->pc_globalctrl);
634 
635 	return (0);
636 }
637 
638 static int
639 ucp_stop_pmc(int cpu, int ri, struct pmc *pm __unused)
640 {
641 
642 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
643 	    ("[uncore,%d] illegal cpu value %d", __LINE__, cpu));
644 	KASSERT(ri >= 0 && ri < uncore_ucp_npmc,
645 	    ("[uncore,%d] illegal row index %d", __LINE__, ri));
646 
647 	PMCDBG2(MDP,STO,1, "ucp-stop cpu=%d ri=%d", cpu, ri);
648 
649 	/* stop hw. */
650 	wrmsr(SELECTSEL(uncore_cputype) + ri, 0);
651 
652 	/* Don't need to write UC_GLOBAL_CTRL, one disable is enough. */
653 
654 	return (0);
655 }
656 
657 static int
658 ucp_write_pmc(int cpu, int ri, struct pmc *pm, pmc_value_t v)
659 {
660 
661 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
662 	    ("[uncore,%d] illegal cpu value %d", __LINE__, cpu));
663 	KASSERT(ri >= 0 && ri < uncore_ucp_npmc,
664 	    ("[uncore,%d] illegal row index %d", __LINE__, ri));
665 
666 	PMCDBG4(MDP,WRI,1, "ucp-write cpu=%d ri=%d msr=0x%x v=%jx", cpu, ri,
667 	    UCP_PMC0 + ri, v);
668 
669 	if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
670 		v = ucp_reload_count_to_perfctr_value(v);
671 
672 	/*
673 	 * Write the new value to the counter.  The counter will be in
674 	 * a stopped state when the pcd_write() entry point is called.
675 	 */
676 
677 	wrmsr(UCP_PMC0 + ri, v);
678 
679 	return (0);
680 }
681 
682 
683 static void
684 ucp_initialize(struct pmc_mdep *md, int maxcpu, int npmc, int pmcwidth)
685 {
686 	struct pmc_classdep *pcd;
687 
688 	KASSERT(md != NULL, ("[ucp,%d] md is NULL", __LINE__));
689 
690 	PMCDBG0(MDP,INI,1, "ucp-initialize");
691 
692 	pcd = &md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCP];
693 
694 	pcd->pcd_caps	= UCP_PMC_CAPS;
695 	pcd->pcd_class	= PMC_CLASS_UCP;
696 	pcd->pcd_num	= npmc;
697 	pcd->pcd_ri	= md->pmd_npmc;
698 	pcd->pcd_width	= pmcwidth;
699 
700 	pcd->pcd_allocate_pmc	= ucp_allocate_pmc;
701 	pcd->pcd_config_pmc	= ucp_config_pmc;
702 	pcd->pcd_describe	= ucp_describe;
703 	pcd->pcd_get_config	= ucp_get_config;
704 	pcd->pcd_get_msr	= NULL;
705 	pcd->pcd_pcpu_fini	= uncore_pcpu_fini;
706 	pcd->pcd_pcpu_init	= uncore_pcpu_init;
707 	pcd->pcd_read_pmc	= ucp_read_pmc;
708 	pcd->pcd_release_pmc	= ucp_release_pmc;
709 	pcd->pcd_start_pmc	= ucp_start_pmc;
710 	pcd->pcd_stop_pmc	= ucp_stop_pmc;
711 	pcd->pcd_write_pmc	= ucp_write_pmc;
712 
713 	md->pmd_npmc	       += npmc;
714 }
715 
716 int
717 pmc_uncore_initialize(struct pmc_mdep *md, int maxcpu)
718 {
719 	uncore_cputype = md->pmd_cputype;
720 	uncore_pmcmask = 0;
721 
722 	/*
723 	 * Initialize programmable counters.
724 	 */
725 
726 	uncore_ucp_npmc  = 8;
727 	uncore_ucp_width = 48;
728 
729 	uncore_pmcmask |= ((1ULL << uncore_ucp_npmc) - 1);
730 
731 	ucp_initialize(md, maxcpu, uncore_ucp_npmc, uncore_ucp_width);
732 
733 	/*
734 	 * Initialize fixed function counters, if present.
735 	 */
736 	uncore_ucf_ri = uncore_ucp_npmc;
737 	uncore_ucf_npmc  = 1;
738 	uncore_ucf_width = 48;
739 
740 	ucf_initialize(md, maxcpu, uncore_ucf_npmc, uncore_ucf_width);
741 	uncore_pmcmask |= ((1ULL << uncore_ucf_npmc) - 1) << SELECTOFF(uncore_cputype);
742 
743 	PMCDBG2(MDP,INI,1,"uncore-init pmcmask=0x%jx ucfri=%d", uncore_pmcmask,
744 	    uncore_ucf_ri);
745 
746 	uncore_pcpu = malloc(sizeof(*uncore_pcpu) * maxcpu, M_PMC,
747 	    M_ZERO | M_WAITOK);
748 
749 	return (0);
750 }
751 
752 void
753 pmc_uncore_finalize(struct pmc_mdep *md)
754 {
755 	PMCDBG0(MDP,INI,1, "uncore-finalize");
756 
757 	free(uncore_pcpu, M_PMC);
758 	uncore_pcpu = NULL;
759 }
760