1e829eb6dSJoseph Koshy /*- 2718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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) { 102e829eb6dSJoseph Koshy case 0x600: /* Pentium Pro, Celeron, Pentium II & III */ 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; 1210cfab8ddSJoseph Koshy case 0x1C: /* Per Intel document 320047-002. */ 1220cfab8ddSJoseph Koshy cputype = PMC_CPU_INTEL_ATOM; 1230cfab8ddSJoseph Koshy nclasses = 3; 1240cfab8ddSJoseph Koshy break; 125597979c4SJeff Roberson case 0x1A: 1264b226201SSean Bruno case 0x1E: /* 1274b226201SSean Bruno * Per Intel document 253669-032 9/2009, 1284b226201SSean Bruno * pages A-2 and A-57 1294b226201SSean Bruno */ 1304b226201SSean Bruno case 0x1F: /* 1314b226201SSean Bruno * Per Intel document 253669-032 9/2009, 1324b226201SSean Bruno * pages A-2 and A-57 1334b226201SSean Bruno */ 134597979c4SJeff Roberson cputype = PMC_CPU_INTEL_COREI7; 1351fa7f10bSFabien Thomas nclasses = 5; 1361fa7f10bSFabien Thomas break; 13749fe48abSKonstantin Belousov case 0x2E: 13849fe48abSKonstantin Belousov cputype = PMC_CPU_INTEL_NEHALEM_EX; 13949fe48abSKonstantin Belousov nclasses = 3; 14049fe48abSKonstantin Belousov break; 1411fa7f10bSFabien Thomas case 0x25: /* Per Intel document 253669-033US 12/2009. */ 1421fa7f10bSFabien Thomas case 0x2C: /* Per Intel document 253669-033US 12/2009. */ 1431fa7f10bSFabien Thomas cputype = PMC_CPU_INTEL_WESTMERE; 1441fa7f10bSFabien Thomas nclasses = 5; 145597979c4SJeff Roberson break; 14649fe48abSKonstantin Belousov case 0x2F: /* Westmere-EX, seen in wild */ 14749fe48abSKonstantin Belousov cputype = PMC_CPU_INTEL_WESTMERE_EX; 14849fe48abSKonstantin Belousov nclasses = 3; 14949fe48abSKonstantin Belousov break; 15078d763a2SDavide Italiano case 0x2A: /* Per Intel document 253669-039US 05/2011. */ 15178d763a2SDavide Italiano cputype = PMC_CPU_INTEL_SANDYBRIDGE; 1524f35e8cbSMitchell Horne nclasses = 3; 15378d763a2SDavide Italiano break; 154fabe02f5SSean Bruno case 0x2D: /* Per Intel document 253669-044US 08/2012. */ 155fabe02f5SSean Bruno cputype = PMC_CPU_INTEL_SANDYBRIDGE_XEON; 156fabe02f5SSean Bruno nclasses = 3; 157fabe02f5SSean Bruno break; 1581e862e5aSFabien Thomas case 0x3A: /* Per Intel document 253669-043US 05/2012. */ 1591e862e5aSFabien Thomas cputype = PMC_CPU_INTEL_IVYBRIDGE; 1601e862e5aSFabien Thomas nclasses = 3; 1611e862e5aSFabien Thomas break; 1623f929d8cSSean Bruno case 0x3E: /* Per Intel document 325462-045US 01/2013. */ 1633f929d8cSSean Bruno cputype = PMC_CPU_INTEL_IVYBRIDGE_XEON; 1643f929d8cSSean Bruno nclasses = 3; 1653f929d8cSSean Bruno break; 166*fe109d31SAlexander Motin case 0x3D: 167*fe109d31SAlexander Motin case 0x47: 168*fe109d31SAlexander Motin cputype = PMC_CPU_INTEL_BROADWELL; 169*fe109d31SAlexander Motin nclasses = 3; 170*fe109d31SAlexander Motin break; 171*fe109d31SAlexander Motin case 0x4f: 172*fe109d31SAlexander Motin case 0x56: 173*fe109d31SAlexander Motin cputype = PMC_CPU_INTEL_BROADWELL_XEON; 174*fe109d31SAlexander Motin nclasses = 3; 175*fe109d31SAlexander Motin break; 176*fe109d31SAlexander Motin case 0x3C: /* Per Intel document 325462-045US 01/2013. */ 177*fe109d31SAlexander Motin case 0x45: /* Per Intel document 325462-045US 09/2014. */ 178*fe109d31SAlexander Motin cputype = PMC_CPU_INTEL_HASWELL; 179*fe109d31SAlexander Motin nclasses = 3; 180*fe109d31SAlexander Motin break; 181*fe109d31SAlexander Motin case 0x3F: /* Per Intel document 325462-045US 09/2014. */ 182*fe109d31SAlexander Motin case 0x46: /* Per Intel document 325462-045US 09/2014. */ 183*fe109d31SAlexander Motin /* Should 46 be XEON. probably its own? */ 184*fe109d31SAlexander Motin cputype = PMC_CPU_INTEL_HASWELL_XEON; 185*fe109d31SAlexander Motin nclasses = 3; 186*fe109d31SAlexander Motin break; 18707ff05c2SRuslan Bukin /* Skylake */ 188f19bae41SRandall Stewart case 0x4e: 189f19bae41SRandall Stewart case 0x5e: 19007ff05c2SRuslan Bukin /* Kabylake */ 19107ff05c2SRuslan Bukin case 0x8E: /* Per Intel document 325462-063US July 2017. */ 19207ff05c2SRuslan Bukin case 0x9E: /* Per Intel document 325462-063US July 2017. */ 193913c07a0SAlexander Motin /* Cometlake */ 194913c07a0SAlexander Motin case 0xA5: 195913c07a0SAlexander Motin case 0xA6: 196f19bae41SRandall Stewart cputype = PMC_CPU_INTEL_SKYLAKE; 197f19bae41SRandall Stewart nclasses = 3; 198f19bae41SRandall Stewart break; 199b99b705dSKonstantin Belousov case 0x55: /* SDM rev 63 */ 200b99b705dSKonstantin Belousov cputype = PMC_CPU_INTEL_SKYLAKE_XEON; 201b99b705dSKonstantin Belousov nclasses = 3; 202b99b705dSKonstantin Belousov break; 203913c07a0SAlexander Motin /* Icelake */ 204913c07a0SAlexander Motin case 0x7D: 205913c07a0SAlexander Motin case 0x7E: 206913c07a0SAlexander Motin /* Tigerlake */ 207913c07a0SAlexander Motin case 0x8C: 208913c07a0SAlexander Motin case 0x8D: 209913c07a0SAlexander Motin /* Rocketlake */ 210913c07a0SAlexander Motin case 0xA7: 211913c07a0SAlexander Motin cputype = PMC_CPU_INTEL_ICELAKE; 212913c07a0SAlexander Motin nclasses = 3; 213913c07a0SAlexander Motin break; 214913c07a0SAlexander Motin case 0x6A: 215913c07a0SAlexander Motin case 0x6C: 216913c07a0SAlexander Motin cputype = PMC_CPU_INTEL_ICELAKE_XEON; 217913c07a0SAlexander Motin nclasses = 3; 218913c07a0SAlexander Motin break; 219*fe109d31SAlexander Motin case 0x97: 220*fe109d31SAlexander Motin case 0x9A: 221*fe109d31SAlexander Motin cputype = PMC_CPU_INTEL_ALDERLAKE; 2224f35e8cbSMitchell Horne nclasses = 3; 223cc0c1555SSean Bruno break; 224d852f79bSKonstantin Belousov case 0x37: 225d852f79bSKonstantin Belousov case 0x4A: 226e8f021a3SHiren Panchasara case 0x4D: /* Per Intel document 330061-001 01/2014. */ 227d852f79bSKonstantin Belousov case 0x5A: 228d852f79bSKonstantin Belousov case 0x5D: 229e8f021a3SHiren Panchasara cputype = PMC_CPU_INTEL_ATOM_SILVERMONT; 230e8f021a3SHiren Panchasara nclasses = 3; 231e8f021a3SHiren Panchasara break; 2328e6d2a15SMarcin Wojtas case 0x5C: /* Per Intel document 325462-071US 10/2019. */ 233bbdddb80SAlexander Motin case 0x5F: 2348e6d2a15SMarcin Wojtas cputype = PMC_CPU_INTEL_ATOM_GOLDMONT; 2358e6d2a15SMarcin Wojtas nclasses = 3; 2368e6d2a15SMarcin Wojtas break; 237e829eb6dSJoseph Koshy } 238e829eb6dSJoseph Koshy break; 239e829eb6dSJoseph Koshy } 240e92a1350SMatt Macy 241e829eb6dSJoseph Koshy 242e829eb6dSJoseph Koshy if ((int) cputype == -1) { 243e829eb6dSJoseph Koshy printf("pmc: Unknown Intel CPU.\n"); 244e829eb6dSJoseph Koshy return (NULL); 245e829eb6dSJoseph Koshy } 246e829eb6dSJoseph Koshy 247f5f9340bSFabien Thomas /* Allocate base class and initialize machine dependent struct */ 248f5f9340bSFabien Thomas pmc_mdep = pmc_mdep_alloc(nclasses); 249e829eb6dSJoseph Koshy 250e829eb6dSJoseph Koshy pmc_mdep->pmd_cputype = cputype; 251e829eb6dSJoseph Koshy pmc_mdep->pmd_switch_in = intel_switch_in; 252e829eb6dSJoseph Koshy pmc_mdep->pmd_switch_out = intel_switch_out; 253e829eb6dSJoseph Koshy 254e829eb6dSJoseph Koshy ncpus = pmc_cpu_max(); 2551c12d03fSDavide Italiano error = pmc_tsc_initialize(pmc_mdep, ncpus); 2561c12d03fSDavide Italiano if (error) 2571c12d03fSDavide Italiano goto error; 258*fe109d31SAlexander Motin 2598399d923SMitchell Horne MPASS(nclasses >= PMC_MDEP_CLASS_INDEX_IAF); 260026346c8SAttilio Rao error = pmc_core_initialize(pmc_mdep, ncpus, verov); 2611c12d03fSDavide Italiano if (error) { 2621c12d03fSDavide Italiano pmc_tsc_finalize(pmc_mdep); 263ef902782SOleksandr Tymoshenko goto error; 2641c12d03fSDavide Italiano } 265ef902782SOleksandr Tymoshenko 2661fa7f10bSFabien Thomas /* 2671fa7f10bSFabien Thomas * Init the uncore class. 2681fa7f10bSFabien Thomas */ 2691fa7f10bSFabien Thomas switch (cputype) { 2701fa7f10bSFabien Thomas /* 2711fa7f10bSFabien Thomas * Intel Corei7 and Westmere processors. 2721fa7f10bSFabien Thomas */ 2731fa7f10bSFabien Thomas case PMC_CPU_INTEL_COREI7: 2741fa7f10bSFabien Thomas case PMC_CPU_INTEL_WESTMERE: 2754f35e8cbSMitchell Horne #ifdef notyet 2764f35e8cbSMitchell Horne /* 2774f35e8cbSMitchell Horne * TODO: re-enable uncore class on these processors. 2784f35e8cbSMitchell Horne * 2794f35e8cbSMitchell Horne * The uncore unit was reworked beginning with Sandy Bridge, including 2804f35e8cbSMitchell Horne * the MSRs required to program it. In particular, we need to: 2814f35e8cbSMitchell Horne * - Parse the MSR_UNC_CBO_CONFIG MSR for number of C-box units in the 2824f35e8cbSMitchell Horne * system 2834f35e8cbSMitchell Horne * - Support reading and writing to ARB and C-box units, depending on 2844f35e8cbSMitchell Horne * the requested event 2854f35e8cbSMitchell Horne * - Create some kind of mapping between C-box <--> CPU 2864f35e8cbSMitchell Horne * 2874f35e8cbSMitchell Horne * Also TODO: support other later changes to these interfaces, to 2884f35e8cbSMitchell Horne * enable the uncore class on generations newer than Broadwell. 2894f35e8cbSMitchell Horne * Skylake+ appears to use newer addresses for the uncore MSRs. 2904f35e8cbSMitchell Horne */ 2914f35e8cbSMitchell Horne case PMC_CPU_INTEL_HASWELL: 292bc346409SRui Paulo case PMC_CPU_INTEL_BROADWELL: 2934f35e8cbSMitchell Horne case PMC_CPU_INTEL_SANDYBRIDGE: 2944f35e8cbSMitchell Horne #endif 2958399d923SMitchell Horne MPASS(nclasses >= PMC_MDEP_CLASS_INDEX_UCF); 2961fa7f10bSFabien Thomas error = pmc_uncore_initialize(pmc_mdep, ncpus); 2971fa7f10bSFabien Thomas break; 2981fa7f10bSFabien Thomas default: 2991fa7f10bSFabien Thomas break; 3001fa7f10bSFabien Thomas } 301e829eb6dSJoseph Koshy error: 302e829eb6dSJoseph Koshy if (error) { 303e1bd42c2SDavide Italiano pmc_mdep_free(pmc_mdep); 304e829eb6dSJoseph Koshy pmc_mdep = NULL; 305e829eb6dSJoseph Koshy } 306e829eb6dSJoseph Koshy 307e829eb6dSJoseph Koshy return (pmc_mdep); 308e829eb6dSJoseph Koshy } 309e829eb6dSJoseph Koshy 310e829eb6dSJoseph Koshy void 311e829eb6dSJoseph Koshy pmc_intel_finalize(struct pmc_mdep *md) 312e829eb6dSJoseph Koshy { 313e829eb6dSJoseph Koshy pmc_tsc_finalize(md); 314e829eb6dSJoseph Koshy 3150cfab8ddSJoseph Koshy pmc_core_finalize(md); 3161fa7f10bSFabien Thomas 3171fa7f10bSFabien Thomas /* 3181fa7f10bSFabien Thomas * Uncore. 3191fa7f10bSFabien Thomas */ 3201fa7f10bSFabien Thomas switch (md->pmd_cputype) { 3211fa7f10bSFabien Thomas case PMC_CPU_INTEL_COREI7: 3221fa7f10bSFabien Thomas case PMC_CPU_INTEL_WESTMERE: 3234f35e8cbSMitchell Horne #ifdef notyet 3244f35e8cbSMitchell Horne case PMC_CPU_INTEL_HASWELL: 3254f35e8cbSMitchell Horne case PMC_CPU_INTEL_BROADWELL: 3264f35e8cbSMitchell Horne case PMC_CPU_INTEL_SANDYBRIDGE: 3274f35e8cbSMitchell Horne #endif 3281fa7f10bSFabien Thomas pmc_uncore_finalize(md); 3291fa7f10bSFabien Thomas break; 3301fa7f10bSFabien Thomas default: 3311fa7f10bSFabien Thomas break; 3321fa7f10bSFabien Thomas } 333e829eb6dSJoseph Koshy } 334