1e829eb6dSJoseph Koshy /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3718cf2ccSPedro F. Giffuni * 4e829eb6dSJoseph Koshy * Copyright (c) 2008 Joseph Koshy 5e829eb6dSJoseph Koshy * All rights reserved. 6e829eb6dSJoseph Koshy * 7e829eb6dSJoseph Koshy * Redistribution and use in source and binary forms, with or without 8e829eb6dSJoseph Koshy * modification, are permitted provided that the following conditions 9e829eb6dSJoseph Koshy * are met: 10e829eb6dSJoseph Koshy * 1. Redistributions of source code must retain the above copyright 11e829eb6dSJoseph Koshy * notice, this list of conditions and the following disclaimer. 12e829eb6dSJoseph Koshy * 2. Redistributions in binary form must reproduce the above copyright 13e829eb6dSJoseph Koshy * notice, this list of conditions and the following disclaimer in the 14e829eb6dSJoseph Koshy * documentation and/or other materials provided with the distribution. 15e829eb6dSJoseph Koshy * 16e829eb6dSJoseph Koshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17e829eb6dSJoseph Koshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18e829eb6dSJoseph Koshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19e829eb6dSJoseph Koshy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20e829eb6dSJoseph Koshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21e829eb6dSJoseph Koshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22e829eb6dSJoseph Koshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23e829eb6dSJoseph Koshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24e829eb6dSJoseph Koshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25e829eb6dSJoseph Koshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26e829eb6dSJoseph Koshy * SUCH DAMAGE. 27e829eb6dSJoseph Koshy */ 28e829eb6dSJoseph Koshy 29e829eb6dSJoseph Koshy /* 30e829eb6dSJoseph Koshy * Common code for handling Intel CPUs. 31e829eb6dSJoseph Koshy */ 32e829eb6dSJoseph Koshy 33e829eb6dSJoseph Koshy #include <sys/cdefs.h> 34e829eb6dSJoseph Koshy __FBSDID("$FreeBSD$"); 35e829eb6dSJoseph Koshy 36e829eb6dSJoseph Koshy #include <sys/param.h> 37e829eb6dSJoseph Koshy #include <sys/pmc.h> 38e829eb6dSJoseph Koshy #include <sys/pmckern.h> 39e829eb6dSJoseph Koshy #include <sys/systm.h> 40e829eb6dSJoseph Koshy 41e829eb6dSJoseph Koshy #include <machine/cpu.h> 425113aa0aSJung-uk Kim #include <machine/cputypes.h> 43e829eb6dSJoseph Koshy #include <machine/md_var.h> 44e829eb6dSJoseph Koshy #include <machine/specialreg.h> 45e829eb6dSJoseph Koshy 46e829eb6dSJoseph Koshy static int 47e829eb6dSJoseph Koshy intel_switch_in(struct pmc_cpu *pc, struct pmc_process *pp) 48e829eb6dSJoseph Koshy { 49e829eb6dSJoseph Koshy (void) pc; 50e829eb6dSJoseph Koshy 514a3690dfSJohn Baldwin PMCDBG3(MDP,SWI,1, "pc=%p pp=%p enable-msr=%d", pc, pp, 52e829eb6dSJoseph Koshy pp->pp_flags & PMC_PP_ENABLE_MSR_ACCESS); 53e829eb6dSJoseph Koshy 54e829eb6dSJoseph Koshy /* allow the RDPMC instruction if needed */ 55e829eb6dSJoseph Koshy if (pp->pp_flags & PMC_PP_ENABLE_MSR_ACCESS) 56e829eb6dSJoseph Koshy load_cr4(rcr4() | CR4_PCE); 57e829eb6dSJoseph Koshy 584a3690dfSJohn Baldwin PMCDBG1(MDP,SWI,1, "cr4=0x%jx", (uintmax_t) rcr4()); 59e829eb6dSJoseph Koshy 60e829eb6dSJoseph Koshy return 0; 61e829eb6dSJoseph Koshy } 62e829eb6dSJoseph Koshy 63e829eb6dSJoseph Koshy static int 64e829eb6dSJoseph Koshy intel_switch_out(struct pmc_cpu *pc, struct pmc_process *pp) 65e829eb6dSJoseph Koshy { 66e829eb6dSJoseph Koshy (void) pc; 67e829eb6dSJoseph Koshy (void) pp; /* can be NULL */ 68e829eb6dSJoseph Koshy 694a3690dfSJohn Baldwin PMCDBG3(MDP,SWO,1, "pc=%p pp=%p cr4=0x%jx", pc, pp, 70e829eb6dSJoseph Koshy (uintmax_t) rcr4()); 71e829eb6dSJoseph Koshy 72e829eb6dSJoseph Koshy /* always turn off the RDPMC instruction */ 73e829eb6dSJoseph Koshy load_cr4(rcr4() & ~CR4_PCE); 74e829eb6dSJoseph Koshy 75e829eb6dSJoseph Koshy return 0; 76e829eb6dSJoseph Koshy } 77e829eb6dSJoseph Koshy 78e829eb6dSJoseph Koshy struct pmc_mdep * 79e829eb6dSJoseph Koshy pmc_intel_initialize(void) 80e829eb6dSJoseph Koshy { 81e829eb6dSJoseph Koshy struct pmc_mdep *pmc_mdep; 82e829eb6dSJoseph Koshy enum pmc_cputype cputype; 83ef013ceeSRyan Moeller int error, family, model, nclasses, ncpus, stepping, verov; 84e829eb6dSJoseph Koshy 855113aa0aSJung-uk Kim KASSERT(cpu_vendor_id == CPU_VENDOR_INTEL, 86e829eb6dSJoseph Koshy ("[intel,%d] Initializing non-intel processor", __LINE__)); 87e829eb6dSJoseph Koshy 884a3690dfSJohn Baldwin PMCDBG1(MDP,INI,0, "intel-initialize cpuid=0x%x", cpu_id); 89e829eb6dSJoseph Koshy 90e829eb6dSJoseph Koshy cputype = -1; 91e829eb6dSJoseph Koshy nclasses = 2; 92e1bd42c2SDavide Italiano error = 0; 93026346c8SAttilio Rao verov = 0; 94ef013ceeSRyan Moeller family = CPUID_TO_FAMILY(cpu_id); 95ef013ceeSRyan Moeller model = CPUID_TO_MODEL(cpu_id); 96ef013ceeSRyan Moeller stepping = CPUID_TO_STEPPING(cpu_id); 970cfab8ddSJoseph Koshy 981791cad0SAlexander Motin snprintf(pmc_cpuid, sizeof(pmc_cpuid), "GenuineIntel-%d-%02X-%X", 99ef013ceeSRyan Moeller family, model, stepping); 100ef013ceeSRyan Moeller 101e829eb6dSJoseph Koshy switch (cpu_id & 0xF00) { 102ca43b2aeSMitchell Horne case 0x600: 1030cfab8ddSJoseph Koshy switch (model) { 1040cfab8ddSJoseph Koshy case 0xE: 1050cfab8ddSJoseph Koshy cputype = PMC_CPU_INTEL_CORE; 1060cfab8ddSJoseph Koshy break; 1070cfab8ddSJoseph Koshy case 0xF: 108026346c8SAttilio Rao /* Per Intel document 315338-020. */ 109026346c8SAttilio Rao if (stepping == 0x7) { 110026346c8SAttilio Rao cputype = PMC_CPU_INTEL_CORE; 111026346c8SAttilio Rao verov = 1; 112026346c8SAttilio Rao } else { 1130cfab8ddSJoseph Koshy cputype = PMC_CPU_INTEL_CORE2; 1140cfab8ddSJoseph Koshy nclasses = 3; 115026346c8SAttilio Rao } 1160cfab8ddSJoseph Koshy break; 1170cfab8ddSJoseph Koshy case 0x17: 1180cfab8ddSJoseph Koshy cputype = PMC_CPU_INTEL_CORE2EXTREME; 1190cfab8ddSJoseph Koshy nclasses = 3; 1200cfab8ddSJoseph Koshy break; 121597979c4SJeff Roberson case 0x1A: 1224b226201SSean Bruno case 0x1E: /* 1234b226201SSean Bruno * Per Intel document 253669-032 9/2009, 1244b226201SSean Bruno * pages A-2 and A-57 1254b226201SSean Bruno */ 1264b226201SSean Bruno case 0x1F: /* 1274b226201SSean Bruno * Per Intel document 253669-032 9/2009, 1284b226201SSean Bruno * pages A-2 and A-57 1294b226201SSean Bruno */ 130597979c4SJeff Roberson cputype = PMC_CPU_INTEL_COREI7; 1311fa7f10bSFabien Thomas nclasses = 5; 1321fa7f10bSFabien Thomas break; 13349fe48abSKonstantin Belousov case 0x2E: 13449fe48abSKonstantin Belousov cputype = PMC_CPU_INTEL_NEHALEM_EX; 13549fe48abSKonstantin Belousov nclasses = 3; 13649fe48abSKonstantin Belousov break; 1371fa7f10bSFabien Thomas case 0x25: /* Per Intel document 253669-033US 12/2009. */ 1381fa7f10bSFabien Thomas case 0x2C: /* Per Intel document 253669-033US 12/2009. */ 1391fa7f10bSFabien Thomas cputype = PMC_CPU_INTEL_WESTMERE; 1401fa7f10bSFabien Thomas nclasses = 5; 141597979c4SJeff Roberson break; 14249fe48abSKonstantin Belousov case 0x2F: /* Westmere-EX, seen in wild */ 14349fe48abSKonstantin Belousov cputype = PMC_CPU_INTEL_WESTMERE_EX; 14449fe48abSKonstantin Belousov nclasses = 3; 14549fe48abSKonstantin Belousov break; 14678d763a2SDavide Italiano case 0x2A: /* Per Intel document 253669-039US 05/2011. */ 14778d763a2SDavide Italiano cputype = PMC_CPU_INTEL_SANDYBRIDGE; 1484f35e8cbSMitchell Horne nclasses = 3; 14978d763a2SDavide Italiano break; 150fabe02f5SSean Bruno case 0x2D: /* Per Intel document 253669-044US 08/2012. */ 151fabe02f5SSean Bruno cputype = PMC_CPU_INTEL_SANDYBRIDGE_XEON; 152fabe02f5SSean Bruno nclasses = 3; 153fabe02f5SSean Bruno break; 1541e862e5aSFabien Thomas case 0x3A: /* Per Intel document 253669-043US 05/2012. */ 1551e862e5aSFabien Thomas cputype = PMC_CPU_INTEL_IVYBRIDGE; 1561e862e5aSFabien Thomas nclasses = 3; 1571e862e5aSFabien Thomas break; 1583f929d8cSSean Bruno case 0x3E: /* Per Intel document 325462-045US 01/2013. */ 1593f929d8cSSean Bruno cputype = PMC_CPU_INTEL_IVYBRIDGE_XEON; 1603f929d8cSSean Bruno nclasses = 3; 1613f929d8cSSean Bruno break; 162fe109d31SAlexander Motin case 0x3D: 163fe109d31SAlexander Motin case 0x47: 164fe109d31SAlexander Motin cputype = PMC_CPU_INTEL_BROADWELL; 165fe109d31SAlexander Motin nclasses = 3; 166fe109d31SAlexander Motin break; 167fe109d31SAlexander Motin case 0x4f: 168fe109d31SAlexander Motin case 0x56: 169fe109d31SAlexander Motin cputype = PMC_CPU_INTEL_BROADWELL_XEON; 170fe109d31SAlexander Motin nclasses = 3; 171fe109d31SAlexander Motin break; 172fe109d31SAlexander Motin case 0x3C: /* Per Intel document 325462-045US 01/2013. */ 173fe109d31SAlexander Motin case 0x45: /* Per Intel document 325462-045US 09/2014. */ 174fe109d31SAlexander Motin cputype = PMC_CPU_INTEL_HASWELL; 175fe109d31SAlexander Motin nclasses = 3; 176fe109d31SAlexander Motin break; 177fe109d31SAlexander Motin case 0x3F: /* Per Intel document 325462-045US 09/2014. */ 178fe109d31SAlexander Motin case 0x46: /* Per Intel document 325462-045US 09/2014. */ 179fe109d31SAlexander Motin /* Should 46 be XEON. probably its own? */ 180fe109d31SAlexander Motin cputype = PMC_CPU_INTEL_HASWELL_XEON; 181fe109d31SAlexander Motin nclasses = 3; 182fe109d31SAlexander Motin break; 18307ff05c2SRuslan Bukin /* Skylake */ 184f19bae41SRandall Stewart case 0x4e: 185f19bae41SRandall Stewart case 0x5e: 18607ff05c2SRuslan Bukin /* Kabylake */ 18707ff05c2SRuslan Bukin case 0x8E: /* Per Intel document 325462-063US July 2017. */ 18807ff05c2SRuslan Bukin case 0x9E: /* Per Intel document 325462-063US July 2017. */ 189913c07a0SAlexander Motin /* Cometlake */ 190913c07a0SAlexander Motin case 0xA5: 191913c07a0SAlexander Motin case 0xA6: 192f19bae41SRandall Stewart cputype = PMC_CPU_INTEL_SKYLAKE; 193f19bae41SRandall Stewart nclasses = 3; 194f19bae41SRandall Stewart break; 195b99b705dSKonstantin Belousov case 0x55: /* SDM rev 63 */ 196b99b705dSKonstantin Belousov cputype = PMC_CPU_INTEL_SKYLAKE_XEON; 197b99b705dSKonstantin Belousov nclasses = 3; 198b99b705dSKonstantin Belousov break; 199913c07a0SAlexander Motin /* Icelake */ 200913c07a0SAlexander Motin case 0x7D: 201913c07a0SAlexander Motin case 0x7E: 202913c07a0SAlexander Motin /* Tigerlake */ 203913c07a0SAlexander Motin case 0x8C: 204913c07a0SAlexander Motin case 0x8D: 205913c07a0SAlexander Motin /* Rocketlake */ 206913c07a0SAlexander Motin case 0xA7: 207913c07a0SAlexander Motin cputype = PMC_CPU_INTEL_ICELAKE; 208913c07a0SAlexander Motin nclasses = 3; 209913c07a0SAlexander Motin break; 210913c07a0SAlexander Motin case 0x6A: 211913c07a0SAlexander Motin case 0x6C: 212913c07a0SAlexander Motin cputype = PMC_CPU_INTEL_ICELAKE_XEON; 213913c07a0SAlexander Motin nclasses = 3; 214913c07a0SAlexander Motin break; 215fe109d31SAlexander Motin case 0x97: 216fe109d31SAlexander Motin case 0x9A: 217fe109d31SAlexander Motin cputype = PMC_CPU_INTEL_ALDERLAKE; 2184f35e8cbSMitchell Horne nclasses = 3; 219cc0c1555SSean Bruno break; 22013260178SAlexander Motin case 0x1C: /* Per Intel document 320047-002. */ 22113260178SAlexander Motin case 0x26: 22213260178SAlexander Motin case 0x27: 22313260178SAlexander Motin case 0x35: 22413260178SAlexander Motin case 0x36: 22513260178SAlexander Motin cputype = PMC_CPU_INTEL_ATOM; 22613260178SAlexander Motin nclasses = 3; 22713260178SAlexander Motin break; 228d852f79bSKonstantin Belousov case 0x37: 229d852f79bSKonstantin Belousov case 0x4A: 230e8f021a3SHiren Panchasara case 0x4D: /* Per Intel document 330061-001 01/2014. */ 231d852f79bSKonstantin Belousov case 0x5A: 232d852f79bSKonstantin Belousov case 0x5D: 233e8f021a3SHiren Panchasara cputype = PMC_CPU_INTEL_ATOM_SILVERMONT; 234e8f021a3SHiren Panchasara nclasses = 3; 235e8f021a3SHiren Panchasara break; 2368e6d2a15SMarcin Wojtas case 0x5C: /* Per Intel document 325462-071US 10/2019. */ 237bbdddb80SAlexander Motin case 0x5F: 2388e6d2a15SMarcin Wojtas cputype = PMC_CPU_INTEL_ATOM_GOLDMONT; 2398e6d2a15SMarcin Wojtas nclasses = 3; 2408e6d2a15SMarcin Wojtas break; 24113260178SAlexander Motin case 0x7A: 24213260178SAlexander Motin cputype = PMC_CPU_INTEL_ATOM_GOLDMONT_P; 24313260178SAlexander Motin nclasses = 3; 24413260178SAlexander Motin break; 24513260178SAlexander Motin case 0x86: 24613260178SAlexander Motin case 0x96: 24713260178SAlexander Motin cputype = PMC_CPU_INTEL_ATOM_TREMONT; 24813260178SAlexander Motin nclasses = 3; 24913260178SAlexander Motin break; 250e829eb6dSJoseph Koshy } 251e829eb6dSJoseph Koshy break; 252e829eb6dSJoseph Koshy } 253e92a1350SMatt Macy 254e829eb6dSJoseph Koshy 255e829eb6dSJoseph Koshy if ((int) cputype == -1) { 256e829eb6dSJoseph Koshy printf("pmc: Unknown Intel CPU.\n"); 257e829eb6dSJoseph Koshy return (NULL); 258e829eb6dSJoseph Koshy } 259e829eb6dSJoseph Koshy 260f5f9340bSFabien Thomas /* Allocate base class and initialize machine dependent struct */ 261f5f9340bSFabien Thomas pmc_mdep = pmc_mdep_alloc(nclasses); 262e829eb6dSJoseph Koshy 263e829eb6dSJoseph Koshy pmc_mdep->pmd_cputype = cputype; 264e829eb6dSJoseph Koshy pmc_mdep->pmd_switch_in = intel_switch_in; 265e829eb6dSJoseph Koshy pmc_mdep->pmd_switch_out = intel_switch_out; 266e829eb6dSJoseph Koshy 267e829eb6dSJoseph Koshy ncpus = pmc_cpu_max(); 2681c12d03fSDavide Italiano error = pmc_tsc_initialize(pmc_mdep, ncpus); 2691c12d03fSDavide Italiano if (error) 2701c12d03fSDavide Italiano goto error; 271fe109d31SAlexander Motin 2728399d923SMitchell Horne MPASS(nclasses >= PMC_MDEP_CLASS_INDEX_IAF); 273026346c8SAttilio Rao error = pmc_core_initialize(pmc_mdep, ncpus, verov); 2741c12d03fSDavide Italiano if (error) { 2751c12d03fSDavide Italiano pmc_tsc_finalize(pmc_mdep); 276ef902782SOleksandr Tymoshenko goto error; 2771c12d03fSDavide Italiano } 278ef902782SOleksandr Tymoshenko 2791fa7f10bSFabien Thomas /* 2801fa7f10bSFabien Thomas * Init the uncore class. 2811fa7f10bSFabien Thomas */ 2821fa7f10bSFabien Thomas switch (cputype) { 2831fa7f10bSFabien Thomas /* 2841fa7f10bSFabien Thomas * Intel Corei7 and Westmere processors. 2851fa7f10bSFabien Thomas */ 2861fa7f10bSFabien Thomas case PMC_CPU_INTEL_COREI7: 2871fa7f10bSFabien Thomas case PMC_CPU_INTEL_WESTMERE: 2884f35e8cbSMitchell Horne #ifdef notyet 2894f35e8cbSMitchell Horne /* 2904f35e8cbSMitchell Horne * TODO: re-enable uncore class on these processors. 2914f35e8cbSMitchell Horne * 2924f35e8cbSMitchell Horne * The uncore unit was reworked beginning with Sandy Bridge, including 2934f35e8cbSMitchell Horne * the MSRs required to program it. In particular, we need to: 2944f35e8cbSMitchell Horne * - Parse the MSR_UNC_CBO_CONFIG MSR for number of C-box units in the 2954f35e8cbSMitchell Horne * system 2964f35e8cbSMitchell Horne * - Support reading and writing to ARB and C-box units, depending on 2974f35e8cbSMitchell Horne * the requested event 2984f35e8cbSMitchell Horne * - Create some kind of mapping between C-box <--> CPU 2994f35e8cbSMitchell Horne * 3004f35e8cbSMitchell Horne * Also TODO: support other later changes to these interfaces, to 3014f35e8cbSMitchell Horne * enable the uncore class on generations newer than Broadwell. 3024f35e8cbSMitchell Horne * Skylake+ appears to use newer addresses for the uncore MSRs. 3034f35e8cbSMitchell Horne */ 3044f35e8cbSMitchell Horne case PMC_CPU_INTEL_HASWELL: 305bc346409SRui Paulo case PMC_CPU_INTEL_BROADWELL: 3064f35e8cbSMitchell Horne case PMC_CPU_INTEL_SANDYBRIDGE: 3074f35e8cbSMitchell Horne #endif 3088399d923SMitchell Horne MPASS(nclasses >= PMC_MDEP_CLASS_INDEX_UCF); 3091fa7f10bSFabien Thomas error = pmc_uncore_initialize(pmc_mdep, ncpus); 3101fa7f10bSFabien Thomas break; 3111fa7f10bSFabien Thomas default: 3121fa7f10bSFabien Thomas break; 3131fa7f10bSFabien Thomas } 314e829eb6dSJoseph Koshy error: 315e829eb6dSJoseph Koshy if (error) { 316e1bd42c2SDavide Italiano pmc_mdep_free(pmc_mdep); 317e829eb6dSJoseph Koshy pmc_mdep = NULL; 318e829eb6dSJoseph Koshy } 319e829eb6dSJoseph Koshy 320e829eb6dSJoseph Koshy return (pmc_mdep); 321e829eb6dSJoseph Koshy } 322e829eb6dSJoseph Koshy 323e829eb6dSJoseph Koshy void 324e829eb6dSJoseph Koshy pmc_intel_finalize(struct pmc_mdep *md) 325e829eb6dSJoseph Koshy { 326e829eb6dSJoseph Koshy pmc_tsc_finalize(md); 327e829eb6dSJoseph Koshy 3280cfab8ddSJoseph Koshy pmc_core_finalize(md); 3291fa7f10bSFabien Thomas 3301fa7f10bSFabien Thomas /* 3311fa7f10bSFabien Thomas * Uncore. 3321fa7f10bSFabien Thomas */ 3331fa7f10bSFabien Thomas switch (md->pmd_cputype) { 3341fa7f10bSFabien Thomas case PMC_CPU_INTEL_COREI7: 3351fa7f10bSFabien Thomas case PMC_CPU_INTEL_WESTMERE: 3364f35e8cbSMitchell Horne #ifdef notyet 3374f35e8cbSMitchell Horne case PMC_CPU_INTEL_HASWELL: 3384f35e8cbSMitchell Horne case PMC_CPU_INTEL_BROADWELL: 3394f35e8cbSMitchell Horne case PMC_CPU_INTEL_SANDYBRIDGE: 3404f35e8cbSMitchell Horne #endif 3411fa7f10bSFabien Thomas pmc_uncore_finalize(md); 3421fa7f10bSFabien Thomas break; 3431fa7f10bSFabien Thomas default: 3441fa7f10bSFabien Thomas break; 3451fa7f10bSFabien Thomas } 346e829eb6dSJoseph Koshy } 347