2 * Copyright (c) 2008 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Common code for handling Intel CPUs.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
36 #include <sys/pmckern.h>
37 #include <sys/systm.h>
39 #include <machine/cpu.h>
40 #include <machine/cputypes.h>
41 #include <machine/md_var.h>
42 #include <machine/specialreg.h>
45 intel_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
49 PMCDBG(MDP,SWI,1, "pc=%p pp=%p enable-msr=%d", pc, pp,
50 pp->pp_flags & PMC_PP_ENABLE_MSR_ACCESS);
52 /* allow the RDPMC instruction if needed */
53 if (pp->pp_flags & PMC_PP_ENABLE_MSR_ACCESS)
54 load_cr4(rcr4() | CR4_PCE);
56 PMCDBG(MDP,SWI,1, "cr4=0x%jx", (uintmax_t) rcr4());
62 intel_switch_out(struct pmc_cpu *pc, struct pmc_process *pp)
65 (void) pp; /* can be NULL */
67 PMCDBG(MDP,SWO,1, "pc=%p pp=%p cr4=0x%jx", pc, pp,
70 /* always turn off the RDPMC instruction */
71 load_cr4(rcr4() & ~CR4_PCE);
77 pmc_intel_initialize(void)
79 struct pmc_mdep *pmc_mdep;
80 enum pmc_cputype cputype;
81 int error, model, nclasses, ncpus;
83 KASSERT(cpu_vendor_id == CPU_VENDOR_INTEL,
84 ("[intel,%d] Initializing non-intel processor", __LINE__));
86 PMCDBG(MDP,INI,0, "intel-initialize cpuid=0x%x", cpu_id);
91 model = ((cpu_id & 0xF0000) >> 12) | ((cpu_id & 0xF0) >> 4);
93 switch (cpu_id & 0xF00) {
95 case 0x500: /* Pentium family processors */
96 cputype = PMC_CPU_INTEL_P5;
99 case 0x600: /* Pentium Pro, Celeron, Pentium II & III */
101 #if defined(__i386__)
103 cputype = PMC_CPU_INTEL_P6;
106 cputype = PMC_CPU_INTEL_PII;
109 cputype = PMC_CPU_INTEL_CL;
111 case 0x7: case 0x8: case 0xA: case 0xB:
112 cputype = PMC_CPU_INTEL_PIII;
115 cputype = PMC_CPU_INTEL_PM;
119 cputype = PMC_CPU_INTEL_CORE;
122 cputype = PMC_CPU_INTEL_CORE2;
126 cputype = PMC_CPU_INTEL_CORE2EXTREME;
129 case 0x1C: /* Per Intel document 320047-002. */
130 cputype = PMC_CPU_INTEL_ATOM;
134 case 0x1E: /* Per Intel document 253669-032 9/2009, pages A-2 and A-57 */
135 case 0x1F: /* Per Intel document 253669-032 9/2009, pages A-2 and A-57 */
137 cputype = PMC_CPU_INTEL_COREI7;
140 case 0x25: /* Per Intel document 253669-033US 12/2009. */
141 case 0x2C: /* Per Intel document 253669-033US 12/2009. */
142 cputype = PMC_CPU_INTEL_WESTMERE;
147 #if defined(__i386__) || defined(__amd64__)
149 if (model >= 0 && model <= 6) /* known models */
150 cputype = PMC_CPU_INTEL_PIV;
155 if ((int) cputype == -1) {
156 printf("pmc: Unknown Intel CPU.\n");
160 pmc_mdep = malloc(sizeof(struct pmc_mdep) + nclasses *
161 sizeof(struct pmc_classdep), M_PMC, M_WAITOK|M_ZERO);
163 pmc_mdep->pmd_cputype = cputype;
164 pmc_mdep->pmd_nclass = nclasses;
166 pmc_mdep->pmd_switch_in = intel_switch_in;
167 pmc_mdep->pmd_switch_out = intel_switch_out;
169 ncpus = pmc_cpu_max();
171 error = pmc_tsc_initialize(pmc_mdep, ncpus);
176 #if defined(__i386__) || defined(__amd64__)
178 * Intel Core, Core 2 and Atom processors.
180 case PMC_CPU_INTEL_ATOM:
181 case PMC_CPU_INTEL_CORE:
182 case PMC_CPU_INTEL_CORE2:
183 case PMC_CPU_INTEL_CORE2EXTREME:
184 case PMC_CPU_INTEL_COREI7:
185 case PMC_CPU_INTEL_WESTMERE:
186 error = pmc_core_initialize(pmc_mdep, ncpus);
190 * Intel Pentium 4 Processors, and P4/EMT64 processors.
193 case PMC_CPU_INTEL_PIV:
194 error = pmc_p4_initialize(pmc_mdep, ncpus);
196 KASSERT(pmc_mdep->pmd_npmc == TSC_NPMCS + P4_NPMCS,
197 ("[intel,%d] incorrect npmc count %d", __LINE__,
198 pmc_mdep->pmd_npmc));
202 #if defined(__i386__)
204 * P6 Family Processors
207 case PMC_CPU_INTEL_P6:
208 case PMC_CPU_INTEL_CL:
209 case PMC_CPU_INTEL_PII:
210 case PMC_CPU_INTEL_PIII:
211 case PMC_CPU_INTEL_PM:
212 error = pmc_p6_initialize(pmc_mdep, ncpus);
214 KASSERT(pmc_mdep->pmd_npmc == TSC_NPMCS + P6_NPMCS,
215 ("[intel,%d] incorrect npmc count %d", __LINE__,
216 pmc_mdep->pmd_npmc));
220 * Intel Pentium PMCs.
223 case PMC_CPU_INTEL_P5:
224 error = pmc_p5_initialize(pmc_mdep, ncpus);
226 KASSERT(pmc_mdep->pmd_npmc == TSC_NPMCS + PENTIUM_NPMCS,
227 ("[intel,%d] incorrect npmc count %d", __LINE__,
228 pmc_mdep->pmd_npmc));
233 KASSERT(0, ("[intel,%d] Unknown CPU type", __LINE__));
237 * Init the uncore class.
239 #if defined(__i386__) || defined(__amd64__)
242 * Intel Corei7 and Westmere processors.
244 case PMC_CPU_INTEL_COREI7:
245 case PMC_CPU_INTEL_WESTMERE:
246 error = pmc_uncore_initialize(pmc_mdep, ncpus);
255 free(pmc_mdep, M_PMC);
263 pmc_intel_finalize(struct pmc_mdep *md)
265 pmc_tsc_finalize(md);
267 switch (md->pmd_cputype) {
268 #if defined(__i386__) || defined(__amd64__)
269 case PMC_CPU_INTEL_ATOM:
270 case PMC_CPU_INTEL_CORE:
271 case PMC_CPU_INTEL_CORE2:
272 case PMC_CPU_INTEL_CORE2EXTREME:
273 case PMC_CPU_INTEL_COREI7:
274 case PMC_CPU_INTEL_WESTMERE:
275 pmc_core_finalize(md);
278 case PMC_CPU_INTEL_PIV:
282 #if defined(__i386__)
283 case PMC_CPU_INTEL_P6:
284 case PMC_CPU_INTEL_CL:
285 case PMC_CPU_INTEL_PII:
286 case PMC_CPU_INTEL_PIII:
287 case PMC_CPU_INTEL_PM:
290 case PMC_CPU_INTEL_P5:
295 KASSERT(0, ("[intel,%d] unknown CPU type", __LINE__));
301 #if defined(__i386__) || defined(__amd64__)
302 switch (md->pmd_cputype) {
303 case PMC_CPU_INTEL_COREI7:
304 case PMC_CPU_INTEL_WESTMERE:
305 pmc_uncore_finalize(md);