2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Joseph Koshy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
39 #include <sys/pmckern.h>
41 #include <sys/systm.h>
43 #include <machine/intr_machdep.h>
44 #include <x86/apicvar.h>
45 #include <machine/cpu.h>
46 #include <machine/cpufunc.h>
47 #include <machine/md_var.h>
48 #include <machine/specialreg.h>
50 #define CORE_CPUID_REQUEST 0xA
51 #define CORE_CPUID_REQUEST_SIZE 0x4
52 #define CORE_CPUID_EAX 0x0
53 #define CORE_CPUID_EBX 0x1
54 #define CORE_CPUID_ECX 0x2
55 #define CORE_CPUID_EDX 0x3
57 #define IAF_PMC_CAPS \
58 (PMC_CAP_READ | PMC_CAP_WRITE | PMC_CAP_INTERRUPT | \
59 PMC_CAP_USER | PMC_CAP_SYSTEM)
60 #define IAF_RI_TO_MSR(RI) ((RI) + (1 << 30))
62 #define IAP_PMC_CAPS (PMC_CAP_INTERRUPT | PMC_CAP_USER | PMC_CAP_SYSTEM | \
63 PMC_CAP_EDGE | PMC_CAP_THRESHOLD | PMC_CAP_READ | PMC_CAP_WRITE | \
64 PMC_CAP_INVERT | PMC_CAP_QUALIFIER | PMC_CAP_PRECISE)
66 #define EV_IS_NOTARCH 0
67 #define EV_IS_ARCH_SUPP 1
68 #define EV_IS_ARCH_NOTSUPP -1
71 * "Architectural" events defined by Intel. The values of these
72 * symbols correspond to positions in the bitmask returned by
73 * the CPUID.0AH instruction.
75 enum core_arch_events {
76 CORE_AE_BRANCH_INSTRUCTION_RETIRED = 5,
77 CORE_AE_BRANCH_MISSES_RETIRED = 6,
78 CORE_AE_INSTRUCTION_RETIRED = 1,
79 CORE_AE_LLC_MISSES = 4,
80 CORE_AE_LLC_REFERENCE = 3,
81 CORE_AE_UNHALTED_REFERENCE_CYCLES = 2,
82 CORE_AE_UNHALTED_CORE_CYCLES = 0
85 static enum pmc_cputype core_cputype;
86 static int core_version;
89 volatile uint32_t pc_iafctrl; /* Fixed function control. */
90 volatile uint64_t pc_globalctrl; /* Global control register. */
91 struct pmc_hw pc_corepmcs[];
94 static struct core_cpu **core_pcpu;
96 static uint32_t core_architectural_events;
97 static uint64_t core_pmcmask;
99 static int core_iaf_ri; /* relative index of fixed counters */
100 static int core_iaf_width;
101 static int core_iaf_npmc;
103 static int core_iap_width;
104 static int core_iap_npmc;
105 static int core_iap_wroffset;
107 static u_int pmc_alloc_refs;
108 static bool pmc_tsx_force_abort_set;
111 core_pcpu_noop(struct pmc_mdep *md, int cpu)
119 core_pcpu_init(struct pmc_mdep *md, int cpu)
124 int core_ri, n, npmc;
126 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
127 ("[iaf,%d] insane cpu number %d", __LINE__, cpu));
129 PMCDBG1(MDP,INI,1,"core-init cpu=%d", cpu);
131 core_ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_IAP].pcd_ri;
132 npmc = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_IAP].pcd_num;
134 if (core_version >= 2)
135 npmc += md->pmd_classdep[PMC_MDEP_CLASS_INDEX_IAF].pcd_num;
137 cc = malloc(sizeof(struct core_cpu) + npmc * sizeof(struct pmc_hw),
138 M_PMC, M_WAITOK | M_ZERO);
143 KASSERT(pc != NULL && cc != NULL,
144 ("[core,%d] NULL per-cpu structures cpu=%d", __LINE__, cpu));
146 for (n = 0, phw = cc->pc_corepmcs; n < npmc; n++, phw++) {
147 phw->phw_state = PMC_PHW_FLAG_IS_ENABLED |
148 PMC_PHW_CPU_TO_STATE(cpu) |
149 PMC_PHW_INDEX_TO_STATE(n + core_ri);
151 pc->pc_hwpmcs[n + core_ri] = phw;
154 if (core_version >= 2) {
155 /* Enable Freezing PMCs on PMI. */
156 wrmsr(MSR_DEBUGCTLMSR, rdmsr(MSR_DEBUGCTLMSR) | 0x1000);
163 core_pcpu_fini(struct pmc_mdep *md, int cpu)
165 int core_ri, n, npmc;
169 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
170 ("[core,%d] insane cpu number (%d)", __LINE__, cpu));
172 PMCDBG1(MDP,INI,1,"core-pcpu-fini cpu=%d", cpu);
174 if ((cc = core_pcpu[cpu]) == NULL)
177 core_pcpu[cpu] = NULL;
181 KASSERT(pc != NULL, ("[core,%d] NULL per-cpu %d state", __LINE__,
184 npmc = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_IAP].pcd_num;
185 core_ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_IAP].pcd_ri;
187 for (n = 0; n < npmc; n++)
188 wrmsr(IAP_EVSEL0 + n, 0);
190 if (core_version >= 2) {
192 npmc += md->pmd_classdep[PMC_MDEP_CLASS_INDEX_IAF].pcd_num;
195 for (n = 0; n < npmc; n++)
196 pc->pc_hwpmcs[n + core_ri] = NULL;
204 * Fixed function counters.
208 iaf_perfctr_value_to_reload_count(pmc_value_t v)
211 /* If the PMC has overflowed, return a reload count of zero. */
212 if ((v & (1ULL << (core_iaf_width - 1))) == 0)
214 v &= (1ULL << core_iaf_width) - 1;
215 return (1ULL << core_iaf_width) - v;
219 iaf_reload_count_to_perfctr_value(pmc_value_t rlc)
221 return (1ULL << core_iaf_width) - rlc;
225 iaf_allocate_pmc(int cpu, int ri, struct pmc *pm,
226 const struct pmc_op_pmcallocate *a)
230 uint64_t config, flags;
231 const struct pmc_md_iap_op_pmcallocate *iap;
233 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
234 ("[core,%d] illegal CPU %d", __LINE__, cpu));
236 PMCDBG2(MDP,ALL,1, "iaf-allocate ri=%d reqcaps=0x%x", ri, pm->pm_caps);
238 if (ri < 0 || ri > core_iaf_npmc)
243 if (a->pm_class != PMC_CLASS_IAF ||
244 (caps & IAF_PMC_CAPS) != caps)
247 iap = &a->pm_md.pm_iap;
248 config = iap->pm_iap_config;
249 ev = IAP_EVSEL_GET(config);
250 umask = IAP_UMASK_GET(config);
257 case 0: /* INST_RETIRED.ANY */
258 if (ev != 0xC0 || umask != 0x00)
261 case 1: /* CPU_CLK_UNHALTED.THREAD */
262 if (ev != 0x3C || umask != 0x00)
265 case 2: /* CPU_CLK_UNHALTED.REF */
266 if (ev != 0x3C || umask != 0x01)
269 case 3: /* TOPDOWN.SLOTS */
270 if (ev != 0xA4 || umask != 0x01)
279 if ((cpu_stdext_feature3 & CPUID_STDEXT3_TSXFA) != 0 &&
280 !pmc_tsx_force_abort_set) {
281 pmc_tsx_force_abort_set = true;
282 x86_msr_op(MSR_TSX_FORCE_ABORT, MSR_OP_RENDEZVOUS_ALL |
283 MSR_OP_WRITE, 1, NULL);
289 if (config & IAP_USR)
291 if (config & IAP_ANY)
293 if (config & IAP_INT)
296 if (caps & PMC_CAP_INTERRUPT)
298 if (caps & PMC_CAP_SYSTEM)
300 if (caps & PMC_CAP_USER)
302 if ((caps & (PMC_CAP_USER | PMC_CAP_SYSTEM)) == 0)
303 flags |= (IAF_OS | IAF_USR);
305 pm->pm_md.pm_iaf.pm_iaf_ctrl = (flags << (ri * 4));
307 PMCDBG1(MDP,ALL,2, "iaf-allocate config=0x%jx",
308 (uintmax_t) pm->pm_md.pm_iaf.pm_iaf_ctrl);
314 iaf_config_pmc(int cpu, int ri, struct pmc *pm)
316 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
317 ("[core,%d] illegal CPU %d", __LINE__, cpu));
319 KASSERT(ri >= 0 && ri < core_iaf_npmc,
320 ("[core,%d] illegal row-index %d", __LINE__, ri));
322 PMCDBG3(MDP,CFG,1, "iaf-config cpu=%d ri=%d pm=%p", cpu, ri, pm);
324 KASSERT(core_pcpu[cpu] != NULL, ("[core,%d] null per-cpu %d", __LINE__,
327 core_pcpu[cpu]->pc_corepmcs[ri + core_iaf_ri].phw_pmc = pm;
333 iaf_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
337 char iaf_name[PMC_NAME_MAX];
339 phw = &core_pcpu[cpu]->pc_corepmcs[ri + core_iaf_ri];
341 (void) snprintf(iaf_name, sizeof(iaf_name), "IAF-%d", ri);
342 if ((error = copystr(iaf_name, pi->pm_name, PMC_NAME_MAX,
346 pi->pm_class = PMC_CLASS_IAF;
348 if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
349 pi->pm_enabled = TRUE;
350 *ppmc = phw->phw_pmc;
352 pi->pm_enabled = FALSE;
360 iaf_get_config(int cpu, int ri, struct pmc **ppm)
362 *ppm = core_pcpu[cpu]->pc_corepmcs[ri + core_iaf_ri].phw_pmc;
368 iaf_get_msr(int ri, uint32_t *msr)
370 KASSERT(ri >= 0 && ri < core_iaf_npmc,
371 ("[iaf,%d] ri %d out of range", __LINE__, ri));
373 *msr = IAF_RI_TO_MSR(ri);
379 iaf_read_pmc(int cpu, int ri, pmc_value_t *v)
384 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
385 ("[core,%d] illegal cpu value %d", __LINE__, cpu));
386 KASSERT(ri >= 0 && ri < core_iaf_npmc,
387 ("[core,%d] illegal row-index %d", __LINE__, ri));
389 pm = core_pcpu[cpu]->pc_corepmcs[ri + core_iaf_ri].phw_pmc;
392 ("[core,%d] cpu %d ri %d(%d) pmc not configured", __LINE__, cpu,
393 ri, ri + core_iaf_ri));
395 tmp = rdpmc(IAF_RI_TO_MSR(ri));
397 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
398 *v = iaf_perfctr_value_to_reload_count(tmp);
400 *v = tmp & ((1ULL << core_iaf_width) - 1);
402 PMCDBG4(MDP,REA,1, "iaf-read cpu=%d ri=%d msr=0x%x -> v=%jx", cpu, ri,
403 IAF_RI_TO_MSR(ri), *v);
409 iaf_release_pmc(int cpu, int ri, struct pmc *pmc)
411 PMCDBG3(MDP,REL,1, "iaf-release cpu=%d ri=%d pm=%p", cpu, ri, pmc);
413 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
414 ("[core,%d] illegal CPU value %d", __LINE__, cpu));
415 KASSERT(ri >= 0 && ri < core_iaf_npmc,
416 ("[core,%d] illegal row-index %d", __LINE__, ri));
418 KASSERT(core_pcpu[cpu]->pc_corepmcs[ri + core_iaf_ri].phw_pmc == NULL,
419 ("[core,%d] PHW pmc non-NULL", __LINE__));
421 MPASS(pmc_alloc_refs > 0);
422 if (pmc_alloc_refs-- == 1 && pmc_tsx_force_abort_set) {
423 pmc_tsx_force_abort_set = false;
424 x86_msr_op(MSR_TSX_FORCE_ABORT, MSR_OP_RENDEZVOUS_ALL |
425 MSR_OP_WRITE, 0, NULL);
432 iaf_start_pmc(int cpu, int ri)
437 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
438 ("[core,%d] illegal CPU value %d", __LINE__, cpu));
439 KASSERT(ri >= 0 && ri < core_iaf_npmc,
440 ("[core,%d] illegal row-index %d", __LINE__, ri));
442 PMCDBG2(MDP,STA,1,"iaf-start cpu=%d ri=%d", cpu, ri);
445 pm = cc->pc_corepmcs[ri + core_iaf_ri].phw_pmc;
447 cc->pc_iafctrl |= pm->pm_md.pm_iaf.pm_iaf_ctrl;
448 wrmsr(IAF_CTRL, cc->pc_iafctrl);
450 cc->pc_globalctrl |= (1ULL << (ri + IAF_OFFSET));
451 wrmsr(IA_GLOBAL_CTRL, cc->pc_globalctrl);
453 PMCDBG4(MDP,STA,1,"iafctrl=%x(%x) globalctrl=%jx(%jx)",
454 cc->pc_iafctrl, (uint32_t) rdmsr(IAF_CTRL),
455 cc->pc_globalctrl, rdmsr(IA_GLOBAL_CTRL));
461 iaf_stop_pmc(int cpu, int ri)
465 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
466 ("[core,%d] illegal CPU value %d", __LINE__, cpu));
467 KASSERT(ri >= 0 && ri < core_iaf_npmc,
468 ("[core,%d] illegal row-index %d", __LINE__, ri));
470 PMCDBG2(MDP,STA,1,"iaf-stop cpu=%d ri=%d", cpu, ri);
474 cc->pc_iafctrl &= ~(IAF_MASK << (ri * 4));
475 wrmsr(IAF_CTRL, cc->pc_iafctrl);
477 /* Don't need to write IA_GLOBAL_CTRL, one disable is enough. */
479 PMCDBG4(MDP,STO,1,"iafctrl=%x(%x) globalctrl=%jx(%jx)",
480 cc->pc_iafctrl, (uint32_t) rdmsr(IAF_CTRL),
481 cc->pc_globalctrl, rdmsr(IA_GLOBAL_CTRL));
487 iaf_write_pmc(int cpu, int ri, pmc_value_t v)
492 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
493 ("[core,%d] illegal cpu value %d", __LINE__, cpu));
494 KASSERT(ri >= 0 && ri < core_iaf_npmc,
495 ("[core,%d] illegal row-index %d", __LINE__, ri));
498 pm = cc->pc_corepmcs[ri + core_iaf_ri].phw_pmc;
501 ("[core,%d] cpu %d ri %d pmc not configured", __LINE__, cpu, ri));
503 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
504 v = iaf_reload_count_to_perfctr_value(v);
506 /* Turn off the fixed counter */
507 wrmsr(IAF_CTRL, cc->pc_iafctrl & ~(IAF_MASK << (ri * 4)));
509 wrmsr(IAF_CTR0 + ri, v & ((1ULL << core_iaf_width) - 1));
511 /* Turn on fixed counters */
512 wrmsr(IAF_CTRL, cc->pc_iafctrl);
514 PMCDBG6(MDP,WRI,1, "iaf-write cpu=%d ri=%d msr=0x%x v=%jx iafctrl=%jx "
515 "pmc=%jx", cpu, ri, IAF_RI_TO_MSR(ri), v,
516 (uintmax_t) rdmsr(IAF_CTRL),
517 (uintmax_t) rdpmc(IAF_RI_TO_MSR(ri)));
524 iaf_initialize(struct pmc_mdep *md, int maxcpu, int npmc, int pmcwidth)
526 struct pmc_classdep *pcd;
528 KASSERT(md != NULL, ("[iaf,%d] md is NULL", __LINE__));
530 PMCDBG0(MDP,INI,1, "iaf-initialize");
532 pcd = &md->pmd_classdep[PMC_MDEP_CLASS_INDEX_IAF];
534 pcd->pcd_caps = IAF_PMC_CAPS;
535 pcd->pcd_class = PMC_CLASS_IAF;
537 pcd->pcd_ri = md->pmd_npmc;
538 pcd->pcd_width = pmcwidth;
540 pcd->pcd_allocate_pmc = iaf_allocate_pmc;
541 pcd->pcd_config_pmc = iaf_config_pmc;
542 pcd->pcd_describe = iaf_describe;
543 pcd->pcd_get_config = iaf_get_config;
544 pcd->pcd_get_msr = iaf_get_msr;
545 pcd->pcd_pcpu_fini = core_pcpu_noop;
546 pcd->pcd_pcpu_init = core_pcpu_noop;
547 pcd->pcd_read_pmc = iaf_read_pmc;
548 pcd->pcd_release_pmc = iaf_release_pmc;
549 pcd->pcd_start_pmc = iaf_start_pmc;
550 pcd->pcd_stop_pmc = iaf_stop_pmc;
551 pcd->pcd_write_pmc = iaf_write_pmc;
553 md->pmd_npmc += npmc;
557 * Intel programmable PMCs.
560 /* Sub fields of UMASK that this event supports. */
561 #define IAP_M_CORE (1 << 0) /* Core specificity */
562 #define IAP_M_AGENT (1 << 1) /* Agent specificity */
563 #define IAP_M_PREFETCH (1 << 2) /* Prefetch */
564 #define IAP_M_MESI (1 << 3) /* MESI */
565 #define IAP_M_SNOOPRESPONSE (1 << 4) /* Snoop response */
566 #define IAP_M_SNOOPTYPE (1 << 5) /* Snoop type */
567 #define IAP_M_TRANSITION (1 << 6) /* Transition */
569 #define IAP_F_CORE (0x3 << 14) /* Core specificity */
570 #define IAP_F_AGENT (0x1 << 13) /* Agent specificity */
571 #define IAP_F_PREFETCH (0x3 << 12) /* Prefetch */
572 #define IAP_F_MESI (0xF << 8) /* MESI */
573 #define IAP_F_SNOOPRESPONSE (0xB << 8) /* Snoop response */
574 #define IAP_F_SNOOPTYPE (0x3 << 8) /* Snoop type */
575 #define IAP_F_TRANSITION (0x1 << 12) /* Transition */
577 #define IAP_PREFETCH_RESERVED (0x2 << 12)
578 #define IAP_CORE_THIS (0x1 << 14)
579 #define IAP_CORE_ALL (0x3 << 14)
580 #define IAP_F_CMASK 0xFF000000
583 iap_perfctr_value_to_reload_count(pmc_value_t v)
586 /* If the PMC has overflowed, return a reload count of zero. */
587 if ((v & (1ULL << (core_iap_width - 1))) == 0)
589 v &= (1ULL << core_iap_width) - 1;
590 return (1ULL << core_iap_width) - v;
594 iap_reload_count_to_perfctr_value(pmc_value_t rlc)
596 return (1ULL << core_iap_width) - rlc;
600 iap_pmc_has_overflowed(int ri)
605 * We treat a Core (i.e., Intel architecture v1) PMC as has
606 * having overflowed if its MSB is zero.
609 return ((v & (1ULL << (core_iap_width - 1))) == 0);
613 iap_event_corei7_ok_on_counter(uint8_t evsel, int ri)
618 /* Events valid only on counter 0, 1. */
631 /* Any row index is ok. */
636 return (mask & (1 << ri));
640 iap_event_westmere_ok_on_counter(uint8_t evsel, int ri)
645 /* Events valid only on counter 0. */
651 /* Events valid only on counter 0, 1. */
659 /* Any row index is ok. */
664 return (mask & (1 << ri));
668 iap_event_sb_sbx_ib_ibx_ok_on_counter(uint8_t evsel, int ri)
673 /* Events valid only on counter 0. */
677 /* Events valid only on counter 1. */
681 /* Events valid only on counter 2. */
687 /* Events valid only on counter 3. */
692 /* Any row index is ok. */
697 return (mask & (1 << ri));
701 iap_event_core_ok_on_counter(uint8_t evsel, int ri)
707 * Events valid only on counter 0.
719 * Events valid only on counter 1.
728 mask = ~0; /* Any row index is ok. */
731 return (mask & (1 << ri));
735 iap_allocate_pmc(int cpu, int ri, struct pmc *pm,
736 const struct pmc_op_pmcallocate *a)
740 const struct pmc_md_iap_op_pmcallocate *iap;
742 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
743 ("[core,%d] illegal CPU %d", __LINE__, cpu));
744 KASSERT(ri >= 0 && ri < core_iap_npmc,
745 ("[core,%d] illegal row-index value %d", __LINE__, ri));
747 /* check requested capabilities */
749 if ((IAP_PMC_CAPS & caps) != caps)
751 iap = &a->pm_md.pm_iap;
752 ev = IAP_EVSEL_GET(iap->pm_iap_config);
754 switch (core_cputype) {
755 case PMC_CPU_INTEL_CORE:
756 case PMC_CPU_INTEL_CORE2:
757 case PMC_CPU_INTEL_CORE2EXTREME:
758 if (iap_event_core_ok_on_counter(ev, ri) == 0)
760 case PMC_CPU_INTEL_COREI7:
761 case PMC_CPU_INTEL_NEHALEM_EX:
762 if (iap_event_corei7_ok_on_counter(ev, ri) == 0)
765 case PMC_CPU_INTEL_WESTMERE:
766 case PMC_CPU_INTEL_WESTMERE_EX:
767 if (iap_event_westmere_ok_on_counter(ev, ri) == 0)
770 case PMC_CPU_INTEL_SANDYBRIDGE:
771 case PMC_CPU_INTEL_SANDYBRIDGE_XEON:
772 case PMC_CPU_INTEL_IVYBRIDGE:
773 case PMC_CPU_INTEL_IVYBRIDGE_XEON:
774 case PMC_CPU_INTEL_HASWELL:
775 case PMC_CPU_INTEL_HASWELL_XEON:
776 case PMC_CPU_INTEL_BROADWELL:
777 case PMC_CPU_INTEL_BROADWELL_XEON:
778 if (iap_event_sb_sbx_ib_ibx_ok_on_counter(ev, ri) == 0)
781 case PMC_CPU_INTEL_ATOM:
782 case PMC_CPU_INTEL_ATOM_SILVERMONT:
783 case PMC_CPU_INTEL_ATOM_GOLDMONT:
784 case PMC_CPU_INTEL_ATOM_GOLDMONT_P:
785 case PMC_CPU_INTEL_ATOM_TREMONT:
786 case PMC_CPU_INTEL_SKYLAKE:
787 case PMC_CPU_INTEL_SKYLAKE_XEON:
788 case PMC_CPU_INTEL_ICELAKE:
789 case PMC_CPU_INTEL_ICELAKE_XEON:
790 case PMC_CPU_INTEL_ALDERLAKE:
795 pm->pm_md.pm_iap.pm_iap_evsel = iap->pm_iap_config;
800 iap_config_pmc(int cpu, int ri, struct pmc *pm)
802 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
803 ("[core,%d] illegal CPU %d", __LINE__, cpu));
805 KASSERT(ri >= 0 && ri < core_iap_npmc,
806 ("[core,%d] illegal row-index %d", __LINE__, ri));
808 PMCDBG3(MDP,CFG,1, "iap-config cpu=%d ri=%d pm=%p", cpu, ri, pm);
810 KASSERT(core_pcpu[cpu] != NULL, ("[core,%d] null per-cpu %d", __LINE__,
813 core_pcpu[cpu]->pc_corepmcs[ri].phw_pmc = pm;
819 iap_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
823 char iap_name[PMC_NAME_MAX];
825 phw = &core_pcpu[cpu]->pc_corepmcs[ri];
827 (void) snprintf(iap_name, sizeof(iap_name), "IAP-%d", ri);
828 if ((error = copystr(iap_name, pi->pm_name, PMC_NAME_MAX,
832 pi->pm_class = PMC_CLASS_IAP;
834 if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
835 pi->pm_enabled = TRUE;
836 *ppmc = phw->phw_pmc;
838 pi->pm_enabled = FALSE;
846 iap_get_config(int cpu, int ri, struct pmc **ppm)
848 *ppm = core_pcpu[cpu]->pc_corepmcs[ri].phw_pmc;
854 iap_get_msr(int ri, uint32_t *msr)
856 KASSERT(ri >= 0 && ri < core_iap_npmc,
857 ("[iap,%d] ri %d out of range", __LINE__, ri));
865 iap_read_pmc(int cpu, int ri, pmc_value_t *v)
870 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
871 ("[core,%d] illegal cpu value %d", __LINE__, cpu));
872 KASSERT(ri >= 0 && ri < core_iap_npmc,
873 ("[core,%d] illegal row-index %d", __LINE__, ri));
875 pm = core_pcpu[cpu]->pc_corepmcs[ri].phw_pmc;
878 ("[core,%d] cpu %d ri %d pmc not configured", __LINE__, cpu,
882 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
883 *v = iap_perfctr_value_to_reload_count(tmp);
885 *v = tmp & ((1ULL << core_iap_width) - 1);
887 PMCDBG4(MDP,REA,1, "iap-read cpu=%d ri=%d msr=0x%x -> v=%jx", cpu, ri,
894 iap_release_pmc(int cpu, int ri, struct pmc *pm)
898 PMCDBG3(MDP,REL,1, "iap-release cpu=%d ri=%d pm=%p", cpu, ri,
901 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
902 ("[core,%d] illegal CPU value %d", __LINE__, cpu));
903 KASSERT(ri >= 0 && ri < core_iap_npmc,
904 ("[core,%d] illegal row-index %d", __LINE__, ri));
906 KASSERT(core_pcpu[cpu]->pc_corepmcs[ri].phw_pmc
907 == NULL, ("[core,%d] PHW pmc non-NULL", __LINE__));
913 iap_start_pmc(int cpu, int ri)
919 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
920 ("[core,%d] illegal CPU value %d", __LINE__, cpu));
921 KASSERT(ri >= 0 && ri < core_iap_npmc,
922 ("[core,%d] illegal row-index %d", __LINE__, ri));
925 pm = cc->pc_corepmcs[ri].phw_pmc;
928 ("[core,%d] starting cpu%d,ri%d with no pmc configured",
931 PMCDBG2(MDP,STA,1, "iap-start cpu=%d ri=%d", cpu, ri);
933 evsel = pm->pm_md.pm_iap.pm_iap_evsel;
935 PMCDBG4(MDP,STA,2, "iap-start/2 cpu=%d ri=%d evselmsr=0x%x evsel=0x%x",
936 cpu, ri, IAP_EVSEL0 + ri, evsel);
938 /* Event specific configuration. */
940 switch (IAP_EVSEL_GET(evsel)) {
942 wrmsr(IA_OFFCORE_RSP0, pm->pm_md.pm_iap.pm_iap_rsp);
945 wrmsr(IA_OFFCORE_RSP1, pm->pm_md.pm_iap.pm_iap_rsp);
951 wrmsr(IAP_EVSEL0 + ri, evsel | IAP_EN);
953 if (core_version >= 2) {
954 cc->pc_globalctrl |= (1ULL << ri);
955 wrmsr(IA_GLOBAL_CTRL, cc->pc_globalctrl);
962 iap_stop_pmc(int cpu, int ri)
964 struct pmc *pm __diagused;
967 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
968 ("[core,%d] illegal cpu value %d", __LINE__, cpu));
969 KASSERT(ri >= 0 && ri < core_iap_npmc,
970 ("[core,%d] illegal row index %d", __LINE__, ri));
973 pm = cc->pc_corepmcs[ri].phw_pmc;
976 ("[core,%d] cpu%d ri%d no configured PMC to stop", __LINE__,
979 PMCDBG2(MDP,STO,1, "iap-stop cpu=%d ri=%d", cpu, ri);
981 wrmsr(IAP_EVSEL0 + ri, 0);
983 /* Don't need to write IA_GLOBAL_CTRL, one disable is enough. */
989 iap_write_pmc(int cpu, int ri, pmc_value_t v)
994 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
995 ("[core,%d] illegal cpu value %d", __LINE__, cpu));
996 KASSERT(ri >= 0 && ri < core_iap_npmc,
997 ("[core,%d] illegal row index %d", __LINE__, ri));
1000 pm = cc->pc_corepmcs[ri].phw_pmc;
1003 ("[core,%d] cpu%d ri%d no configured PMC to stop", __LINE__,
1006 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1007 v = iap_reload_count_to_perfctr_value(v);
1009 v &= (1ULL << core_iap_width) - 1;
1011 PMCDBG4(MDP,WRI,1, "iap-write cpu=%d ri=%d msr=0x%x v=%jx", cpu, ri,
1015 * Write the new value to the counter (or it's alias). The
1016 * counter will be in a stopped state when the pcd_write()
1017 * entry point is called.
1019 wrmsr(core_iap_wroffset + IAP_PMC0 + ri, v);
1025 iap_initialize(struct pmc_mdep *md, int maxcpu, int npmc, int pmcwidth,
1028 struct pmc_classdep *pcd;
1030 KASSERT(md != NULL, ("[iap,%d] md is NULL", __LINE__));
1032 PMCDBG0(MDP,INI,1, "iap-initialize");
1034 /* Remember the set of architectural events supported. */
1035 core_architectural_events = ~flags;
1037 pcd = &md->pmd_classdep[PMC_MDEP_CLASS_INDEX_IAP];
1039 pcd->pcd_caps = IAP_PMC_CAPS;
1040 pcd->pcd_class = PMC_CLASS_IAP;
1041 pcd->pcd_num = npmc;
1042 pcd->pcd_ri = md->pmd_npmc;
1043 pcd->pcd_width = pmcwidth;
1045 pcd->pcd_allocate_pmc = iap_allocate_pmc;
1046 pcd->pcd_config_pmc = iap_config_pmc;
1047 pcd->pcd_describe = iap_describe;
1048 pcd->pcd_get_config = iap_get_config;
1049 pcd->pcd_get_msr = iap_get_msr;
1050 pcd->pcd_pcpu_fini = core_pcpu_fini;
1051 pcd->pcd_pcpu_init = core_pcpu_init;
1052 pcd->pcd_read_pmc = iap_read_pmc;
1053 pcd->pcd_release_pmc = iap_release_pmc;
1054 pcd->pcd_start_pmc = iap_start_pmc;
1055 pcd->pcd_stop_pmc = iap_stop_pmc;
1056 pcd->pcd_write_pmc = iap_write_pmc;
1058 md->pmd_npmc += npmc;
1062 core_intr(struct trapframe *tf)
1066 struct core_cpu *cc;
1067 int error, found_interrupt, ri;
1069 PMCDBG3(MDP,INT, 1, "cpu=%d tf=%p um=%d", curcpu, (void *) tf,
1070 TRAPF_USERMODE(tf));
1072 found_interrupt = 0;
1073 cc = core_pcpu[curcpu];
1075 for (ri = 0; ri < core_iap_npmc; ri++) {
1077 if ((pm = cc->pc_corepmcs[ri].phw_pmc) == NULL ||
1078 !PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1081 if (!iap_pmc_has_overflowed(ri))
1084 found_interrupt = 1;
1086 if (pm->pm_state != PMC_STATE_RUNNING)
1089 error = pmc_process_interrupt(PMC_HR, pm, tf);
1091 v = pm->pm_sc.pm_reloadcount;
1092 v = iap_reload_count_to_perfctr_value(v);
1095 * Stop the counter, reload it but only restart it if
1096 * the PMC is not stalled.
1098 wrmsr(IAP_EVSEL0 + ri, pm->pm_md.pm_iap.pm_iap_evsel);
1099 wrmsr(core_iap_wroffset + IAP_PMC0 + ri, v);
1101 if (__predict_false(error))
1104 wrmsr(IAP_EVSEL0 + ri, pm->pm_md.pm_iap.pm_iap_evsel | IAP_EN);
1107 if (found_interrupt)
1108 counter_u64_add(pmc_stats.pm_intr_processed, 1);
1110 counter_u64_add(pmc_stats.pm_intr_ignored, 1);
1112 if (found_interrupt)
1113 lapic_reenable_pmc();
1115 return (found_interrupt);
1119 core2_intr(struct trapframe *tf)
1121 int error, found_interrupt = 0, n, cpu;
1122 uint64_t flag, intrstatus, intrdisable = 0;
1124 struct core_cpu *cc;
1128 PMCDBG3(MDP,INT, 1, "cpu=%d tf=0x%p um=%d", cpu, (void *) tf,
1129 TRAPF_USERMODE(tf));
1132 * The IA_GLOBAL_STATUS (MSR 0x38E) register indicates which
1133 * PMCs have a pending PMI interrupt. We take a 'snapshot' of
1134 * the current set of interrupting PMCs and process these
1135 * after stopping them.
1137 intrstatus = rdmsr(IA_GLOBAL_STATUS);
1138 PMCDBG2(MDP,INT, 1, "cpu=%d intrstatus=%jx", cpu,
1139 (uintmax_t) intrstatus);
1142 * Stop PMCs unless hardware already done it.
1144 if ((intrstatus & IA_GLOBAL_STATUS_FLAG_CTR_FRZ) == 0)
1145 wrmsr(IA_GLOBAL_CTRL, 0);
1147 cc = core_pcpu[cpu];
1148 KASSERT(cc != NULL, ("[core,%d] null pcpu", __LINE__));
1151 * Look for interrupts from fixed function PMCs.
1153 for (n = 0, flag = (1ULL << IAF_OFFSET); n < core_iaf_npmc;
1156 if ((intrstatus & flag) == 0)
1159 found_interrupt = 1;
1161 pm = cc->pc_corepmcs[n + core_iaf_ri].phw_pmc;
1162 if (pm == NULL || pm->pm_state != PMC_STATE_RUNNING ||
1163 !PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1166 error = pmc_process_interrupt(PMC_HR, pm, tf);
1167 if (__predict_false(error))
1168 intrdisable |= flag;
1170 v = iaf_reload_count_to_perfctr_value(pm->pm_sc.pm_reloadcount);
1172 /* Reload sampling count. */
1173 wrmsr(IAF_CTR0 + n, v);
1175 PMCDBG4(MDP,INT, 1, "iaf-intr cpu=%d error=%d v=%jx(%jx)", curcpu,
1176 error, (uintmax_t) v, (uintmax_t) rdpmc(IAF_RI_TO_MSR(n)));
1180 * Process interrupts from the programmable counters.
1182 for (n = 0, flag = 1; n < core_iap_npmc; n++, flag <<= 1) {
1183 if ((intrstatus & flag) == 0)
1186 found_interrupt = 1;
1188 pm = cc->pc_corepmcs[n].phw_pmc;
1189 if (pm == NULL || pm->pm_state != PMC_STATE_RUNNING ||
1190 !PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1193 error = pmc_process_interrupt(PMC_HR, pm, tf);
1194 if (__predict_false(error))
1195 intrdisable |= flag;
1197 v = iap_reload_count_to_perfctr_value(pm->pm_sc.pm_reloadcount);
1199 PMCDBG3(MDP,INT, 1, "iap-intr cpu=%d error=%d v=%jx", cpu, error,
1202 /* Reload sampling count. */
1203 wrmsr(core_iap_wroffset + IAP_PMC0 + n, v);
1206 if (found_interrupt)
1207 counter_u64_add(pmc_stats.pm_intr_processed, 1);
1209 counter_u64_add(pmc_stats.pm_intr_ignored, 1);
1211 if (found_interrupt)
1212 lapic_reenable_pmc();
1215 * Reenable all non-stalled PMCs.
1217 if ((intrstatus & IA_GLOBAL_STATUS_FLAG_CTR_FRZ) == 0) {
1218 wrmsr(IA_GLOBAL_OVF_CTRL, intrstatus);
1219 cc->pc_globalctrl &= ~intrdisable;
1220 wrmsr(IA_GLOBAL_CTRL, cc->pc_globalctrl);
1222 if (__predict_false(intrdisable)) {
1223 cc->pc_globalctrl &= ~intrdisable;
1224 wrmsr(IA_GLOBAL_CTRL, cc->pc_globalctrl);
1226 wrmsr(IA_GLOBAL_OVF_CTRL, intrstatus);
1229 PMCDBG4(MDP, INT, 1, "cpu=%d fixedctrl=%jx globalctrl=%jx status=%jx",
1230 cpu, (uintmax_t) rdmsr(IAF_CTRL),
1231 (uintmax_t) rdmsr(IA_GLOBAL_CTRL),
1232 (uintmax_t) rdmsr(IA_GLOBAL_STATUS));
1234 return (found_interrupt);
1238 pmc_core_initialize(struct pmc_mdep *md, int maxcpu, int version_override)
1240 int cpuid[CORE_CPUID_REQUEST_SIZE];
1243 do_cpuid(CORE_CPUID_REQUEST, cpuid);
1245 core_cputype = md->pmd_cputype;
1246 core_version = (version_override > 0) ? version_override :
1247 cpuid[CORE_CPUID_EAX] & 0xFF;
1249 PMCDBG3(MDP,INI,1,"core-init cputype=%d ncpu=%d version=%d",
1250 core_cputype, maxcpu, core_version);
1252 if (core_version < 1 || core_version > 5 ||
1253 (core_cputype != PMC_CPU_INTEL_CORE && core_version == 1)) {
1254 /* Unknown PMC architecture. */
1255 printf("hwpc_core: unknown PMC architecture: %d\n",
1257 return (EPROGMISMATCH);
1260 core_iap_wroffset = 0;
1261 if (cpu_feature2 & CPUID2_PDCM) {
1262 if (rdmsr(IA32_PERF_CAPABILITIES) & PERFCAP_FW_WRITE) {
1263 PMCDBG0(MDP, INI, 1,
1264 "core-init full-width write supported");
1265 core_iap_wroffset = IAP_A_PMC0 - IAP_PMC0;
1267 PMCDBG0(MDP, INI, 1,
1268 "core-init full-width write NOT supported");
1270 PMCDBG0(MDP, INI, 1, "core-init pdcm not supported");
1275 * Initialize programmable counters.
1277 core_iap_npmc = (cpuid[CORE_CPUID_EAX] >> 8) & 0xFF;
1278 core_iap_width = (cpuid[CORE_CPUID_EAX] >> 16) & 0xFF;
1280 core_pmcmask |= ((1ULL << core_iap_npmc) - 1);
1282 nflags = (cpuid[CORE_CPUID_EAX] >> 24) & 0xFF;
1283 flags = cpuid[CORE_CPUID_EBX] & ((1 << nflags) - 1);
1285 iap_initialize(md, maxcpu, core_iap_npmc, core_iap_width, flags);
1288 * Initialize fixed function counters, if present.
1290 if (core_version >= 2) {
1291 core_iaf_ri = core_iap_npmc;
1292 core_iaf_npmc = cpuid[CORE_CPUID_EDX] & 0x1F;
1293 core_iaf_width = (cpuid[CORE_CPUID_EDX] >> 5) & 0xFF;
1295 iaf_initialize(md, maxcpu, core_iaf_npmc, core_iaf_width);
1296 core_pmcmask |= ((1ULL << core_iaf_npmc) - 1) << IAF_OFFSET;
1299 PMCDBG2(MDP,INI,1,"core-init pmcmask=0x%jx iafri=%d", core_pmcmask,
1302 core_pcpu = malloc(sizeof(*core_pcpu) * maxcpu, M_PMC,
1306 * Choose the appropriate interrupt handler.
1308 if (core_version >= 2)
1309 md->pmd_intr = core2_intr;
1311 md->pmd_intr = core_intr;
1313 md->pmd_pcpu_fini = NULL;
1314 md->pmd_pcpu_init = NULL;
1320 pmc_core_finalize(struct pmc_mdep *md)
1322 PMCDBG0(MDP,INI,1, "core-finalize");
1324 free(core_pcpu, M_PMC);