]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/hwpmc/hwpmc_core.c
hwpmc: Reenable PME before reenabling counters.
[FreeBSD/FreeBSD.git] / sys / dev / hwpmc / hwpmc_core.c
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 /*
30  * Intel Core 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/smp.h>
41 #include <sys/systm.h>
42
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>
49
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
56
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))
61
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)
65
66 #define EV_IS_NOTARCH           0
67 #define EV_IS_ARCH_SUPP         1
68 #define EV_IS_ARCH_NOTSUPP      -1
69
70 /*
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.
74  */
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
83 };
84
85 static enum pmc_cputype core_cputype;
86 static int core_version;
87
88 struct core_cpu {
89         volatile uint32_t       pc_iafctrl;     /* Fixed function control. */
90         volatile uint64_t       pc_globalctrl;  /* Global control register. */
91         struct pmc_hw           pc_corepmcs[];
92 };
93
94 static struct core_cpu **core_pcpu;
95
96 static uint32_t core_architectural_events;
97 static uint64_t core_pmcmask;
98
99 static int core_iaf_ri;         /* relative index of fixed counters */
100 static int core_iaf_width;
101 static int core_iaf_npmc;
102
103 static int core_iap_width;
104 static int core_iap_npmc;
105 static int core_iap_wroffset;
106
107 static u_int pmc_alloc_refs;
108 static bool pmc_tsx_force_abort_set;
109
110 static int
111 core_pcpu_noop(struct pmc_mdep *md, int cpu)
112 {
113         (void) md;
114         (void) cpu;
115         return (0);
116 }
117
118 static int
119 core_pcpu_init(struct pmc_mdep *md, int cpu)
120 {
121         struct pmc_cpu *pc;
122         struct core_cpu *cc;
123         struct pmc_hw *phw;
124         int core_ri, n, npmc;
125
126         KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
127             ("[iaf,%d] insane cpu number %d", __LINE__, cpu));
128
129         PMCDBG1(MDP,INI,1,"core-init cpu=%d", cpu);
130
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;
133
134         if (core_version >= 2)
135                 npmc += md->pmd_classdep[PMC_MDEP_CLASS_INDEX_IAF].pcd_num;
136
137         cc = malloc(sizeof(struct core_cpu) + npmc * sizeof(struct pmc_hw),
138             M_PMC, M_WAITOK | M_ZERO);
139
140         core_pcpu[cpu] = cc;
141         pc = pmc_pcpu[cpu];
142
143         KASSERT(pc != NULL && cc != NULL,
144             ("[core,%d] NULL per-cpu structures cpu=%d", __LINE__, cpu));
145
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);
150                 phw->phw_pmc      = NULL;
151                 pc->pc_hwpmcs[n + core_ri]  = phw;
152         }
153
154         if (core_version >= 2) {
155                 /* Enable Freezing PMCs on PMI. */
156                 wrmsr(MSR_DEBUGCTLMSR, rdmsr(MSR_DEBUGCTLMSR) | 0x1000);
157         }
158
159         return (0);
160 }
161
162 static int
163 core_pcpu_fini(struct pmc_mdep *md, int cpu)
164 {
165         int core_ri, n, npmc;
166         struct pmc_cpu *pc;
167         struct core_cpu *cc;
168
169         KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
170             ("[core,%d] insane cpu number (%d)", __LINE__, cpu));
171
172         PMCDBG1(MDP,INI,1,"core-pcpu-fini cpu=%d", cpu);
173
174         if ((cc = core_pcpu[cpu]) == NULL)
175                 return (0);
176
177         core_pcpu[cpu] = NULL;
178
179         pc = pmc_pcpu[cpu];
180
181         KASSERT(pc != NULL, ("[core,%d] NULL per-cpu %d state", __LINE__,
182                 cpu));
183
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;
186
187         for (n = 0; n < npmc; n++)
188                 wrmsr(IAP_EVSEL0 + n, 0);
189
190         if (core_version >= 2) {
191                 wrmsr(IAF_CTRL, 0);
192                 npmc += md->pmd_classdep[PMC_MDEP_CLASS_INDEX_IAF].pcd_num;
193         }
194
195         for (n = 0; n < npmc; n++)
196                 pc->pc_hwpmcs[n + core_ri] = NULL;
197
198         free(cc, M_PMC);
199
200         return (0);
201 }
202
203 /*
204  * Fixed function counters.
205  */
206
207 static pmc_value_t
208 iaf_perfctr_value_to_reload_count(pmc_value_t v)
209 {
210
211         /* If the PMC has overflowed, return a reload count of zero. */
212         if ((v & (1ULL << (core_iaf_width - 1))) == 0)
213                 return (0);
214         v &= (1ULL << core_iaf_width) - 1;
215         return (1ULL << core_iaf_width) - v;
216 }
217
218 static pmc_value_t
219 iaf_reload_count_to_perfctr_value(pmc_value_t rlc)
220 {
221         return (1ULL << core_iaf_width) - rlc;
222 }
223
224 static int
225 iaf_allocate_pmc(int cpu, int ri, struct pmc *pm,
226     const struct pmc_op_pmcallocate *a)
227 {
228         uint8_t ev, umask;
229         uint32_t caps;
230         uint64_t config, flags;
231         const struct pmc_md_iap_op_pmcallocate *iap;
232
233         KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
234             ("[core,%d] illegal CPU %d", __LINE__, cpu));
235
236         PMCDBG2(MDP,ALL,1, "iaf-allocate ri=%d reqcaps=0x%x", ri, pm->pm_caps);
237
238         if (ri < 0 || ri > core_iaf_npmc)
239                 return (EINVAL);
240
241         caps = a->pm_caps;
242
243         if (a->pm_class != PMC_CLASS_IAF ||
244             (caps & IAF_PMC_CAPS) != caps)
245                 return (EINVAL);
246
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);
251
252         if (ev == 0x0) {
253                 if (umask != ri + 1)
254                         return (EINVAL);
255         } else {
256                 switch (ri) {
257                 case 0: /* INST_RETIRED.ANY */
258                         if (ev != 0xC0 || umask != 0x00)
259                                 return (EINVAL);
260                         break;
261                 case 1: /* CPU_CLK_UNHALTED.THREAD */
262                         if (ev != 0x3C || umask != 0x00)
263                                 return (EINVAL);
264                         break;
265                 case 2: /* CPU_CLK_UNHALTED.REF */
266                         if (ev != 0x3C || umask != 0x01)
267                                 return (EINVAL);
268                         break;
269                 case 3: /* TOPDOWN.SLOTS */
270                         if (ev != 0xA4 || umask != 0x01)
271                                 return (EINVAL);
272                         break;
273                 default:
274                         return (EINVAL);
275                 }
276         }
277
278         pmc_alloc_refs++;
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);
284         }
285
286         flags = 0;
287         if (config & IAP_OS)
288                 flags |= IAF_OS;
289         if (config & IAP_USR)
290                 flags |= IAF_USR;
291         if (config & IAP_ANY)
292                 flags |= IAF_ANY;
293         if (config & IAP_INT)
294                 flags |= IAF_PMI;
295
296         if (caps & PMC_CAP_INTERRUPT)
297                 flags |= IAF_PMI;
298         if (caps & PMC_CAP_SYSTEM)
299                 flags |= IAF_OS;
300         if (caps & PMC_CAP_USER)
301                 flags |= IAF_USR;
302         if ((caps & (PMC_CAP_USER | PMC_CAP_SYSTEM)) == 0)
303                 flags |= (IAF_OS | IAF_USR);
304
305         pm->pm_md.pm_iaf.pm_iaf_ctrl = (flags << (ri * 4));
306
307         PMCDBG1(MDP,ALL,2, "iaf-allocate config=0x%jx",
308             (uintmax_t) pm->pm_md.pm_iaf.pm_iaf_ctrl);
309
310         return (0);
311 }
312
313 static int
314 iaf_config_pmc(int cpu, int ri, struct pmc *pm)
315 {
316         KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
317             ("[core,%d] illegal CPU %d", __LINE__, cpu));
318
319         KASSERT(ri >= 0 && ri < core_iaf_npmc,
320             ("[core,%d] illegal row-index %d", __LINE__, ri));
321
322         PMCDBG3(MDP,CFG,1, "iaf-config cpu=%d ri=%d pm=%p", cpu, ri, pm);
323
324         KASSERT(core_pcpu[cpu] != NULL, ("[core,%d] null per-cpu %d", __LINE__,
325             cpu));
326
327         core_pcpu[cpu]->pc_corepmcs[ri + core_iaf_ri].phw_pmc = pm;
328
329         return (0);
330 }
331
332 static int
333 iaf_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
334 {
335         int error;
336         struct pmc_hw *phw;
337         char iaf_name[PMC_NAME_MAX];
338
339         phw = &core_pcpu[cpu]->pc_corepmcs[ri + core_iaf_ri];
340
341         (void) snprintf(iaf_name, sizeof(iaf_name), "IAF-%d", ri);
342         if ((error = copystr(iaf_name, pi->pm_name, PMC_NAME_MAX,
343             NULL)) != 0)
344                 return (error);
345
346         pi->pm_class = PMC_CLASS_IAF;
347
348         if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
349                 pi->pm_enabled = TRUE;
350                 *ppmc          = phw->phw_pmc;
351         } else {
352                 pi->pm_enabled = FALSE;
353                 *ppmc          = NULL;
354         }
355
356         return (0);
357 }
358
359 static int
360 iaf_get_config(int cpu, int ri, struct pmc **ppm)
361 {
362         *ppm = core_pcpu[cpu]->pc_corepmcs[ri + core_iaf_ri].phw_pmc;
363
364         return (0);
365 }
366
367 static int
368 iaf_get_msr(int ri, uint32_t *msr)
369 {
370         KASSERT(ri >= 0 && ri < core_iaf_npmc,
371             ("[iaf,%d] ri %d out of range", __LINE__, ri));
372
373         *msr = IAF_RI_TO_MSR(ri);
374
375         return (0);
376 }
377
378 static int
379 iaf_read_pmc(int cpu, int ri, pmc_value_t *v)
380 {
381         struct pmc *pm;
382         pmc_value_t tmp;
383
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));
388
389         pm = core_pcpu[cpu]->pc_corepmcs[ri + core_iaf_ri].phw_pmc;
390
391         KASSERT(pm,
392             ("[core,%d] cpu %d ri %d(%d) pmc not configured", __LINE__, cpu,
393                 ri, ri + core_iaf_ri));
394
395         tmp = rdpmc(IAF_RI_TO_MSR(ri));
396
397         if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
398                 *v = iaf_perfctr_value_to_reload_count(tmp);
399         else
400                 *v = tmp & ((1ULL << core_iaf_width) - 1);
401
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);
404
405         return (0);
406 }
407
408 static int
409 iaf_release_pmc(int cpu, int ri, struct pmc *pmc)
410 {
411         PMCDBG3(MDP,REL,1, "iaf-release cpu=%d ri=%d pm=%p", cpu, ri, pmc);
412
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));
417
418         KASSERT(core_pcpu[cpu]->pc_corepmcs[ri + core_iaf_ri].phw_pmc == NULL,
419             ("[core,%d] PHW pmc non-NULL", __LINE__));
420
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);
426         }
427
428         return (0);
429 }
430
431 static int
432 iaf_start_pmc(int cpu, int ri)
433 {
434         struct pmc *pm;
435         struct core_cpu *cc;
436
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));
441
442         PMCDBG2(MDP,STA,1,"iaf-start cpu=%d ri=%d", cpu, ri);
443
444         cc = core_pcpu[cpu];
445         pm = cc->pc_corepmcs[ri + core_iaf_ri].phw_pmc;
446
447         cc->pc_iafctrl |= pm->pm_md.pm_iaf.pm_iaf_ctrl;
448         wrmsr(IAF_CTRL, cc->pc_iafctrl);
449
450         cc->pc_globalctrl |= (1ULL << (ri + IAF_OFFSET));
451         wrmsr(IA_GLOBAL_CTRL, cc->pc_globalctrl);
452
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));
456
457         return (0);
458 }
459
460 static int
461 iaf_stop_pmc(int cpu, int ri)
462 {
463         struct core_cpu *cc;
464
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));
469
470         PMCDBG2(MDP,STA,1,"iaf-stop cpu=%d ri=%d", cpu, ri);
471
472         cc = core_pcpu[cpu];
473
474         cc->pc_iafctrl &= ~(IAF_MASK << (ri * 4));
475         wrmsr(IAF_CTRL, cc->pc_iafctrl);
476
477         /* Don't need to write IA_GLOBAL_CTRL, one disable is enough. */
478
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));
482
483         return (0);
484 }
485
486 static int
487 iaf_write_pmc(int cpu, int ri, pmc_value_t v)
488 {
489         struct core_cpu *cc;
490         struct pmc *pm;
491
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));
496
497         cc = core_pcpu[cpu];
498         pm = cc->pc_corepmcs[ri + core_iaf_ri].phw_pmc;
499
500         KASSERT(pm,
501             ("[core,%d] cpu %d ri %d pmc not configured", __LINE__, cpu, ri));
502
503         if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
504                 v = iaf_reload_count_to_perfctr_value(v);
505
506         /* Turn off the fixed counter */
507         wrmsr(IAF_CTRL, cc->pc_iafctrl & ~(IAF_MASK << (ri * 4)));
508
509         wrmsr(IAF_CTR0 + ri, v & ((1ULL << core_iaf_width) - 1));
510
511         /* Turn on fixed counters */
512         wrmsr(IAF_CTRL, cc->pc_iafctrl);
513
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)));
518
519         return (0);
520 }
521
522
523 static void
524 iaf_initialize(struct pmc_mdep *md, int maxcpu, int npmc, int pmcwidth)
525 {
526         struct pmc_classdep *pcd;
527
528         KASSERT(md != NULL, ("[iaf,%d] md is NULL", __LINE__));
529
530         PMCDBG0(MDP,INI,1, "iaf-initialize");
531
532         pcd = &md->pmd_classdep[PMC_MDEP_CLASS_INDEX_IAF];
533
534         pcd->pcd_caps   = IAF_PMC_CAPS;
535         pcd->pcd_class  = PMC_CLASS_IAF;
536         pcd->pcd_num    = npmc;
537         pcd->pcd_ri     = md->pmd_npmc;
538         pcd->pcd_width  = pmcwidth;
539
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;
552
553         md->pmd_npmc           += npmc;
554 }
555
556 /*
557  * Intel programmable PMCs.
558  */
559
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 */
568
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 */
576
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
581
582 static pmc_value_t
583 iap_perfctr_value_to_reload_count(pmc_value_t v)
584 {
585
586         /* If the PMC has overflowed, return a reload count of zero. */
587         if ((v & (1ULL << (core_iap_width - 1))) == 0)
588                 return (0);
589         v &= (1ULL << core_iap_width) - 1;
590         return (1ULL << core_iap_width) - v;
591 }
592
593 static pmc_value_t
594 iap_reload_count_to_perfctr_value(pmc_value_t rlc)
595 {
596         return (1ULL << core_iap_width) - rlc;
597 }
598
599 static int
600 iap_pmc_has_overflowed(int ri)
601 {
602         uint64_t v;
603
604         /*
605          * We treat a Core (i.e., Intel architecture v1) PMC as has
606          * having overflowed if its MSB is zero.
607          */
608         v = rdpmc(ri);
609         return ((v & (1ULL << (core_iap_width - 1))) == 0);
610 }
611
612 static int
613 iap_event_corei7_ok_on_counter(uint8_t evsel, int ri)
614 {
615         uint32_t mask;
616
617         switch (evsel) {
618         /* Events valid only on counter 0, 1. */
619         case 0x40:
620         case 0x41:
621         case 0x42:
622         case 0x43:
623         case 0x4C:
624         case 0x4E:
625         case 0x51:
626         case 0x52:
627         case 0x53:
628         case 0x63:
629                 mask = 0x3;
630                 break;
631         /* Any row index is ok. */
632         default:
633                 mask = ~0;
634         }
635
636         return (mask & (1 << ri));
637 }
638
639 static int
640 iap_event_westmere_ok_on_counter(uint8_t evsel, int ri)
641 {
642         uint32_t mask;
643
644         switch (evsel) {
645         /* Events valid only on counter 0. */
646         case 0x60:
647         case 0xB3:
648                 mask = 0x1;
649                 break;
650
651         /* Events valid only on counter 0, 1. */
652         case 0x4C:
653         case 0x4E:
654         case 0x51:
655         case 0x52:
656         case 0x63:
657                 mask = 0x3;
658                 break;
659         /* Any row index is ok. */
660         default:
661                 mask = ~0;
662         }
663
664         return (mask & (1 << ri));
665 }
666
667 static int
668 iap_event_sb_sbx_ib_ibx_ok_on_counter(uint8_t evsel, int ri)
669 {
670         uint32_t mask;
671
672         switch (evsel) {
673         /* Events valid only on counter 0. */
674         case 0xB7:
675                 mask = 0x1;
676                 break;
677         /* Events valid only on counter 1. */
678         case 0xC0:
679                 mask = 0x2;
680                 break;
681         /* Events valid only on counter 2. */
682         case 0x48:
683         case 0xA2:
684         case 0xA3:
685                 mask = 0x4;
686                 break;
687         /* Events valid only on counter 3. */
688         case 0xBB:
689         case 0xCD:
690                 mask = 0x8;
691                 break;
692         /* Any row index is ok. */
693         default:
694                 mask = ~0;
695         }
696
697         return (mask & (1 << ri));
698 }
699
700 static int
701 iap_event_core_ok_on_counter(uint8_t evsel, int ri)
702 {
703         uint32_t mask;
704
705         switch (evsel) {
706                 /*
707                  * Events valid only on counter 0.
708                  */
709         case 0x10:
710         case 0x14:
711         case 0x18:
712         case 0xB3:
713         case 0xC1:
714         case 0xCB:
715                 mask = (1 << 0);
716                 break;
717
718                 /*
719                  * Events valid only on counter 1.
720                  */
721         case 0x11:
722         case 0x12:
723         case 0x13:
724                 mask = (1 << 1);
725                 break;
726
727         default:
728                 mask = ~0;      /* Any row index is ok. */
729         }
730
731         return (mask & (1 << ri));
732 }
733
734 static int
735 iap_allocate_pmc(int cpu, int ri, struct pmc *pm,
736     const struct pmc_op_pmcallocate *a)
737 {
738         uint8_t ev;
739         uint32_t caps;
740         const struct pmc_md_iap_op_pmcallocate *iap;
741
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));
746
747         /* check requested capabilities */
748         caps = a->pm_caps;
749         if ((IAP_PMC_CAPS & caps) != caps)
750                 return (EPERM);
751         iap = &a->pm_md.pm_iap;
752         ev = IAP_EVSEL_GET(iap->pm_iap_config);
753
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)
759                         return (EINVAL);
760         case PMC_CPU_INTEL_COREI7:
761         case PMC_CPU_INTEL_NEHALEM_EX:
762                 if (iap_event_corei7_ok_on_counter(ev, ri) == 0)
763                         return (EINVAL);
764                 break;
765         case PMC_CPU_INTEL_WESTMERE:
766         case PMC_CPU_INTEL_WESTMERE_EX:
767                 if (iap_event_westmere_ok_on_counter(ev, ri) == 0)
768                         return (EINVAL);
769                 break;
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)
779                         return (EINVAL);
780                 break;
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:
791         default:
792                 break;
793         }
794
795         pm->pm_md.pm_iap.pm_iap_evsel = iap->pm_iap_config;
796         return (0);
797 }
798
799 static int
800 iap_config_pmc(int cpu, int ri, struct pmc *pm)
801 {
802         KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
803             ("[core,%d] illegal CPU %d", __LINE__, cpu));
804
805         KASSERT(ri >= 0 && ri < core_iap_npmc,
806             ("[core,%d] illegal row-index %d", __LINE__, ri));
807
808         PMCDBG3(MDP,CFG,1, "iap-config cpu=%d ri=%d pm=%p", cpu, ri, pm);
809
810         KASSERT(core_pcpu[cpu] != NULL, ("[core,%d] null per-cpu %d", __LINE__,
811             cpu));
812
813         core_pcpu[cpu]->pc_corepmcs[ri].phw_pmc = pm;
814
815         return (0);
816 }
817
818 static int
819 iap_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc)
820 {
821         int error;
822         struct pmc_hw *phw;
823         char iap_name[PMC_NAME_MAX];
824
825         phw = &core_pcpu[cpu]->pc_corepmcs[ri];
826
827         (void) snprintf(iap_name, sizeof(iap_name), "IAP-%d", ri);
828         if ((error = copystr(iap_name, pi->pm_name, PMC_NAME_MAX,
829             NULL)) != 0)
830                 return (error);
831
832         pi->pm_class = PMC_CLASS_IAP;
833
834         if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) {
835                 pi->pm_enabled = TRUE;
836                 *ppmc          = phw->phw_pmc;
837         } else {
838                 pi->pm_enabled = FALSE;
839                 *ppmc          = NULL;
840         }
841
842         return (0);
843 }
844
845 static int
846 iap_get_config(int cpu, int ri, struct pmc **ppm)
847 {
848         *ppm = core_pcpu[cpu]->pc_corepmcs[ri].phw_pmc;
849
850         return (0);
851 }
852
853 static int
854 iap_get_msr(int ri, uint32_t *msr)
855 {
856         KASSERT(ri >= 0 && ri < core_iap_npmc,
857             ("[iap,%d] ri %d out of range", __LINE__, ri));
858
859         *msr = ri;
860
861         return (0);
862 }
863
864 static int
865 iap_read_pmc(int cpu, int ri, pmc_value_t *v)
866 {
867         struct pmc *pm;
868         pmc_value_t tmp;
869
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));
874
875         pm = core_pcpu[cpu]->pc_corepmcs[ri].phw_pmc;
876
877         KASSERT(pm,
878             ("[core,%d] cpu %d ri %d pmc not configured", __LINE__, cpu,
879                 ri));
880
881         tmp = rdpmc(ri);
882         if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
883                 *v = iap_perfctr_value_to_reload_count(tmp);
884         else
885                 *v = tmp & ((1ULL << core_iap_width) - 1);
886
887         PMCDBG4(MDP,REA,1, "iap-read cpu=%d ri=%d msr=0x%x -> v=%jx", cpu, ri,
888             IAP_PMC0 + ri, *v);
889
890         return (0);
891 }
892
893 static int
894 iap_release_pmc(int cpu, int ri, struct pmc *pm)
895 {
896         (void) pm;
897
898         PMCDBG3(MDP,REL,1, "iap-release cpu=%d ri=%d pm=%p", cpu, ri,
899             pm);
900
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));
905
906         KASSERT(core_pcpu[cpu]->pc_corepmcs[ri].phw_pmc
907             == NULL, ("[core,%d] PHW pmc non-NULL", __LINE__));
908
909         return (0);
910 }
911
912 static int
913 iap_start_pmc(int cpu, int ri)
914 {
915         struct pmc *pm;
916         uint64_t evsel;
917         struct core_cpu *cc;
918
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));
923
924         cc = core_pcpu[cpu];
925         pm = cc->pc_corepmcs[ri].phw_pmc;
926
927         KASSERT(pm,
928             ("[core,%d] starting cpu%d,ri%d with no pmc configured",
929                 __LINE__, cpu, ri));
930
931         PMCDBG2(MDP,STA,1, "iap-start cpu=%d ri=%d", cpu, ri);
932
933         evsel = pm->pm_md.pm_iap.pm_iap_evsel;
934
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);
937
938         /* Event specific configuration. */
939
940         switch (IAP_EVSEL_GET(evsel)) {
941         case 0xB7:
942                 wrmsr(IA_OFFCORE_RSP0, pm->pm_md.pm_iap.pm_iap_rsp);
943                 break;
944         case 0xBB:
945                 wrmsr(IA_OFFCORE_RSP1, pm->pm_md.pm_iap.pm_iap_rsp);
946                 break;
947         default:
948                 break;
949         }
950
951         wrmsr(IAP_EVSEL0 + ri, evsel | IAP_EN);
952
953         if (core_version >= 2) {
954                 cc->pc_globalctrl |= (1ULL << ri);
955                 wrmsr(IA_GLOBAL_CTRL, cc->pc_globalctrl);
956         }
957
958         return (0);
959 }
960
961 static int
962 iap_stop_pmc(int cpu, int ri)
963 {
964         struct pmc *pm __diagused;
965         struct core_cpu *cc;
966
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));
971
972         cc = core_pcpu[cpu];
973         pm = cc->pc_corepmcs[ri].phw_pmc;
974
975         KASSERT(pm,
976             ("[core,%d] cpu%d ri%d no configured PMC to stop", __LINE__,
977                 cpu, ri));
978
979         PMCDBG2(MDP,STO,1, "iap-stop cpu=%d ri=%d", cpu, ri);
980
981         wrmsr(IAP_EVSEL0 + ri, 0);
982
983         /* Don't need to write IA_GLOBAL_CTRL, one disable is enough. */
984
985         return (0);
986 }
987
988 static int
989 iap_write_pmc(int cpu, int ri, pmc_value_t v)
990 {
991         struct pmc *pm;
992         struct core_cpu *cc;
993
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));
998
999         cc = core_pcpu[cpu];
1000         pm = cc->pc_corepmcs[ri].phw_pmc;
1001
1002         KASSERT(pm,
1003             ("[core,%d] cpu%d ri%d no configured PMC to stop", __LINE__,
1004                 cpu, ri));
1005
1006         if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1007                 v = iap_reload_count_to_perfctr_value(v);
1008
1009         v &= (1ULL << core_iap_width) - 1;
1010
1011         PMCDBG4(MDP,WRI,1, "iap-write cpu=%d ri=%d msr=0x%x v=%jx", cpu, ri,
1012             IAP_PMC0 + ri, v);
1013
1014         /*
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.
1018          */
1019         wrmsr(core_iap_wroffset + IAP_PMC0 + ri, v);
1020         return (0);
1021 }
1022
1023
1024 static void
1025 iap_initialize(struct pmc_mdep *md, int maxcpu, int npmc, int pmcwidth,
1026     int flags)
1027 {
1028         struct pmc_classdep *pcd;
1029
1030         KASSERT(md != NULL, ("[iap,%d] md is NULL", __LINE__));
1031
1032         PMCDBG0(MDP,INI,1, "iap-initialize");
1033
1034         /* Remember the set of architectural events supported. */
1035         core_architectural_events = ~flags;
1036
1037         pcd = &md->pmd_classdep[PMC_MDEP_CLASS_INDEX_IAP];
1038
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;
1044
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;
1057
1058         md->pmd_npmc           += npmc;
1059 }
1060
1061 static int
1062 core_intr(struct trapframe *tf)
1063 {
1064         pmc_value_t v;
1065         struct pmc *pm;
1066         struct core_cpu *cc;
1067         int error, found_interrupt, ri;
1068
1069         PMCDBG3(MDP,INT, 1, "cpu=%d tf=%p um=%d", curcpu, (void *) tf,
1070             TRAPF_USERMODE(tf));
1071
1072         found_interrupt = 0;
1073         cc = core_pcpu[curcpu];
1074
1075         for (ri = 0; ri < core_iap_npmc; ri++) {
1076
1077                 if ((pm = cc->pc_corepmcs[ri].phw_pmc) == NULL ||
1078                     !PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
1079                         continue;
1080
1081                 if (!iap_pmc_has_overflowed(ri))
1082                         continue;
1083
1084                 found_interrupt = 1;
1085
1086                 if (pm->pm_state != PMC_STATE_RUNNING)
1087                         continue;
1088
1089                 error = pmc_process_interrupt(PMC_HR, pm, tf);
1090
1091                 v = pm->pm_sc.pm_reloadcount;
1092                 v = iap_reload_count_to_perfctr_value(v);
1093
1094                 /*
1095                  * Stop the counter, reload it but only restart it if
1096                  * the PMC is not stalled.
1097                  */
1098                 wrmsr(IAP_EVSEL0 + ri, pm->pm_md.pm_iap.pm_iap_evsel);
1099                 wrmsr(core_iap_wroffset + IAP_PMC0 + ri, v);
1100
1101                 if (__predict_false(error))
1102                         continue;
1103
1104                 wrmsr(IAP_EVSEL0 + ri, pm->pm_md.pm_iap.pm_iap_evsel | IAP_EN);
1105         }
1106
1107         if (found_interrupt)
1108                 counter_u64_add(pmc_stats.pm_intr_processed, 1);
1109         else
1110                 counter_u64_add(pmc_stats.pm_intr_ignored, 1);
1111
1112         if (found_interrupt)
1113                 lapic_reenable_pmc();
1114
1115         return (found_interrupt);
1116 }
1117
1118 static int
1119 core2_intr(struct trapframe *tf)
1120 {
1121         int error, found_interrupt = 0, n, cpu;
1122         uint64_t flag, intrstatus, intrdisable = 0;
1123         struct pmc *pm;
1124         struct core_cpu *cc;
1125         pmc_value_t v;
1126
1127         cpu = curcpu;
1128         PMCDBG3(MDP,INT, 1, "cpu=%d tf=0x%p um=%d", cpu, (void *) tf,
1129             TRAPF_USERMODE(tf));
1130
1131         /*
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.
1136          */
1137         intrstatus = rdmsr(IA_GLOBAL_STATUS);
1138         PMCDBG2(MDP,INT, 1, "cpu=%d intrstatus=%jx", cpu,
1139             (uintmax_t) intrstatus);
1140
1141         /*
1142          * Stop PMCs unless hardware already done it.
1143          */
1144         if ((intrstatus & IA_GLOBAL_STATUS_FLAG_CTR_FRZ) == 0)
1145                 wrmsr(IA_GLOBAL_CTRL, 0);
1146
1147         cc = core_pcpu[cpu];
1148         KASSERT(cc != NULL, ("[core,%d] null pcpu", __LINE__));
1149
1150         /*
1151          * Look for interrupts from fixed function PMCs.
1152          */
1153         for (n = 0, flag = (1ULL << IAF_OFFSET); n < core_iaf_npmc;
1154              n++, flag <<= 1) {
1155
1156                 if ((intrstatus & flag) == 0)
1157                         continue;
1158
1159                 found_interrupt = 1;
1160
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)))
1164                         continue;
1165
1166                 error = pmc_process_interrupt(PMC_HR, pm, tf);
1167                 if (__predict_false(error))
1168                         intrdisable |= flag;
1169
1170                 v = iaf_reload_count_to_perfctr_value(pm->pm_sc.pm_reloadcount);
1171
1172                 /* Reload sampling count. */
1173                 wrmsr(IAF_CTR0 + n, v);
1174
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)));
1177         }
1178
1179         /*
1180          * Process interrupts from the programmable counters.
1181          */
1182         for (n = 0, flag = 1; n < core_iap_npmc; n++, flag <<= 1) {
1183                 if ((intrstatus & flag) == 0)
1184                         continue;
1185
1186                 found_interrupt = 1;
1187
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)))
1191                         continue;
1192
1193                 error = pmc_process_interrupt(PMC_HR, pm, tf);
1194                 if (__predict_false(error))
1195                         intrdisable |= flag;
1196
1197                 v = iap_reload_count_to_perfctr_value(pm->pm_sc.pm_reloadcount);
1198
1199                 PMCDBG3(MDP,INT, 1, "iap-intr cpu=%d error=%d v=%jx", cpu, error,
1200                     (uintmax_t) v);
1201
1202                 /* Reload sampling count. */
1203                 wrmsr(core_iap_wroffset + IAP_PMC0 + n, v);
1204         }
1205
1206         if (found_interrupt)
1207                 counter_u64_add(pmc_stats.pm_intr_processed, 1);
1208         else
1209                 counter_u64_add(pmc_stats.pm_intr_ignored, 1);
1210
1211         if (found_interrupt)
1212                 lapic_reenable_pmc();
1213
1214         /*
1215          * Reenable all non-stalled PMCs.
1216          */
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);
1221         } else {
1222                 if (__predict_false(intrdisable)) {
1223                         cc->pc_globalctrl &= ~intrdisable;
1224                         wrmsr(IA_GLOBAL_CTRL, cc->pc_globalctrl);
1225                 }
1226                 wrmsr(IA_GLOBAL_OVF_CTRL, intrstatus);
1227         }
1228
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));
1233
1234         return (found_interrupt);
1235 }
1236
1237 int
1238 pmc_core_initialize(struct pmc_mdep *md, int maxcpu, int version_override)
1239 {
1240         int cpuid[CORE_CPUID_REQUEST_SIZE];
1241         int flags, nflags;
1242
1243         do_cpuid(CORE_CPUID_REQUEST, cpuid);
1244
1245         core_cputype = md->pmd_cputype;
1246         core_version = (version_override > 0) ? version_override :
1247             cpuid[CORE_CPUID_EAX] & 0xFF;
1248
1249         PMCDBG3(MDP,INI,1,"core-init cputype=%d ncpu=%d version=%d",
1250             core_cputype, maxcpu, core_version);
1251
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",
1256                     core_version);
1257                 return (EPROGMISMATCH);
1258         }
1259
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;
1266                 } else
1267                         PMCDBG0(MDP, INI, 1,
1268                             "core-init full-width write NOT supported");
1269         } else
1270                 PMCDBG0(MDP, INI, 1, "core-init pdcm not supported");
1271
1272         core_pmcmask = 0;
1273
1274         /*
1275          * Initialize programmable counters.
1276          */
1277         core_iap_npmc = (cpuid[CORE_CPUID_EAX] >> 8) & 0xFF;
1278         core_iap_width = (cpuid[CORE_CPUID_EAX] >> 16) & 0xFF;
1279
1280         core_pmcmask |= ((1ULL << core_iap_npmc) - 1);
1281
1282         nflags = (cpuid[CORE_CPUID_EAX] >> 24) & 0xFF;
1283         flags = cpuid[CORE_CPUID_EBX] & ((1 << nflags) - 1);
1284
1285         iap_initialize(md, maxcpu, core_iap_npmc, core_iap_width, flags);
1286
1287         /*
1288          * Initialize fixed function counters, if present.
1289          */
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;
1294
1295                 iaf_initialize(md, maxcpu, core_iaf_npmc, core_iaf_width);
1296                 core_pmcmask |= ((1ULL << core_iaf_npmc) - 1) << IAF_OFFSET;
1297         }
1298
1299         PMCDBG2(MDP,INI,1,"core-init pmcmask=0x%jx iafri=%d", core_pmcmask,
1300             core_iaf_ri);
1301
1302         core_pcpu = malloc(sizeof(*core_pcpu) * maxcpu, M_PMC,
1303             M_ZERO | M_WAITOK);
1304
1305         /*
1306          * Choose the appropriate interrupt handler.
1307          */
1308         if (core_version >= 2)
1309                 md->pmd_intr = core2_intr;
1310         else
1311                 md->pmd_intr = core_intr;
1312
1313         md->pmd_pcpu_fini = NULL;
1314         md->pmd_pcpu_init = NULL;
1315
1316         return (0);
1317 }
1318
1319 void
1320 pmc_core_finalize(struct pmc_mdep *md)
1321 {
1322         PMCDBG0(MDP,INI,1, "core-finalize");
1323
1324         free(core_pcpu, M_PMC);
1325         core_pcpu = NULL;
1326 }