hwpmc_intel.c revision 267602
1/*-
2 * Copyright (c) 2008 Joseph Koshy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27/*
28 * Common code for handling Intel CPUs.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: stable/10/sys/dev/hwpmc/hwpmc_intel.c 267602 2014-06-18 05:35:09Z kib $");
33
34#include <sys/param.h>
35#include <sys/pmc.h>
36#include <sys/pmckern.h>
37#include <sys/systm.h>
38
39#include <machine/cpu.h>
40#include <machine/cputypes.h>
41#include <machine/md_var.h>
42#include <machine/specialreg.h>
43
44static int
45intel_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
46{
47	(void) pc;
48
49	PMCDBG(MDP,SWI,1, "pc=%p pp=%p enable-msr=%d", pc, pp,
50	    pp->pp_flags & PMC_PP_ENABLE_MSR_ACCESS);
51
52	/* allow the RDPMC instruction if needed */
53	if (pp->pp_flags & PMC_PP_ENABLE_MSR_ACCESS)
54		load_cr4(rcr4() | CR4_PCE);
55
56	PMCDBG(MDP,SWI,1, "cr4=0x%jx", (uintmax_t) rcr4());
57
58	return 0;
59}
60
61static int
62intel_switch_out(struct pmc_cpu *pc, struct pmc_process *pp)
63{
64	(void) pc;
65	(void) pp;		/* can be NULL */
66
67	PMCDBG(MDP,SWO,1, "pc=%p pp=%p cr4=0x%jx", pc, pp,
68	    (uintmax_t) rcr4());
69
70	/* always turn off the RDPMC instruction */
71	load_cr4(rcr4() & ~CR4_PCE);
72
73	return 0;
74}
75
76struct pmc_mdep *
77pmc_intel_initialize(void)
78{
79	struct pmc_mdep *pmc_mdep;
80	enum pmc_cputype cputype;
81	int error, model, nclasses, ncpus;
82
83	KASSERT(cpu_vendor_id == CPU_VENDOR_INTEL,
84	    ("[intel,%d] Initializing non-intel processor", __LINE__));
85
86	PMCDBG(MDP,INI,0, "intel-initialize cpuid=0x%x", cpu_id);
87
88	cputype = -1;
89	nclasses = 2;
90	error = 0;
91	model = ((cpu_id & 0xF0000) >> 12) | ((cpu_id & 0xF0) >> 4);
92
93	switch (cpu_id & 0xF00) {
94#if	defined(__i386__)
95	case 0x500:		/* Pentium family processors */
96		cputype = PMC_CPU_INTEL_P5;
97		break;
98#endif
99	case 0x600:		/* Pentium Pro, Celeron, Pentium II & III */
100		switch (model) {
101#if	defined(__i386__)
102		case 0x1:
103			cputype = PMC_CPU_INTEL_P6;
104			break;
105		case 0x3: case 0x5:
106			cputype = PMC_CPU_INTEL_PII;
107			break;
108		case 0x6: case 0x16:
109			cputype = PMC_CPU_INTEL_CL;
110			break;
111		case 0x7: case 0x8: case 0xA: case 0xB:
112			cputype = PMC_CPU_INTEL_PIII;
113			break;
114		case 0x9: case 0xD:
115			cputype = PMC_CPU_INTEL_PM;
116			break;
117#endif
118		case 0xE:
119			cputype = PMC_CPU_INTEL_CORE;
120			break;
121		case 0xF:
122			cputype = PMC_CPU_INTEL_CORE2;
123			nclasses = 3;
124			break;
125		case 0x17:
126			cputype = PMC_CPU_INTEL_CORE2EXTREME;
127			nclasses = 3;
128			break;
129		case 0x1C:	/* Per Intel document 320047-002. */
130			cputype = PMC_CPU_INTEL_ATOM;
131			nclasses = 3;
132			break;
133		case 0x1A:
134		case 0x1E:	/*
135				 * Per Intel document 253669-032 9/2009,
136				 * pages A-2 and A-57
137				 */
138		case 0x1F:	/*
139				 * Per Intel document 253669-032 9/2009,
140				 * pages A-2 and A-57
141				 */
142			cputype = PMC_CPU_INTEL_COREI7;
143			nclasses = 5;
144			break;
145		case 0x2E:
146			cputype = PMC_CPU_INTEL_NEHALEM_EX;
147			nclasses = 3;
148			break;
149		case 0x25:	/* Per Intel document 253669-033US 12/2009. */
150		case 0x2C:	/* Per Intel document 253669-033US 12/2009. */
151			cputype = PMC_CPU_INTEL_WESTMERE;
152			nclasses = 5;
153			break;
154		case 0x2F:	/* Westmere-EX, seen in wild */
155			cputype = PMC_CPU_INTEL_WESTMERE_EX;
156			nclasses = 3;
157			break;
158		case 0x2A:	/* Per Intel document 253669-039US 05/2011. */
159			cputype = PMC_CPU_INTEL_SANDYBRIDGE;
160			nclasses = 5;
161			break;
162		case 0x2D:	/* Per Intel document 253669-044US 08/2012. */
163			cputype = PMC_CPU_INTEL_SANDYBRIDGE_XEON;
164			nclasses = 3;
165			break;
166		case 0x3A:	/* Per Intel document 253669-043US 05/2012. */
167			cputype = PMC_CPU_INTEL_IVYBRIDGE;
168			nclasses = 3;
169			break;
170		case 0x3E:	/* Per Intel document 325462-045US 01/2013. */
171			cputype = PMC_CPU_INTEL_IVYBRIDGE_XEON;
172			nclasses = 3;
173			break;
174		case 0x3C:	/* Per Intel document 325462-045US 01/2013. */
175			cputype = PMC_CPU_INTEL_HASWELL;
176			nclasses = 5;
177			break;
178		case 0x4D:      /* Per Intel document 330061-001 01/2014. */
179			cputype = PMC_CPU_INTEL_ATOM_SILVERMONT;
180			nclasses = 3;
181			break;
182		}
183		break;
184#if	defined(__i386__) || defined(__amd64__)
185	case 0xF00:		/* P4 */
186		if (model >= 0 && model <= 6) /* known models */
187			cputype = PMC_CPU_INTEL_PIV;
188		break;
189	}
190#endif
191
192	if ((int) cputype == -1) {
193		printf("pmc: Unknown Intel CPU.\n");
194		return (NULL);
195	}
196
197	/* Allocate base class and initialize machine dependent struct */
198	pmc_mdep = pmc_mdep_alloc(nclasses);
199
200	pmc_mdep->pmd_cputype	 = cputype;
201	pmc_mdep->pmd_switch_in	 = intel_switch_in;
202	pmc_mdep->pmd_switch_out = intel_switch_out;
203
204	ncpus = pmc_cpu_max();
205	error = pmc_tsc_initialize(pmc_mdep, ncpus);
206	if (error)
207		goto error;
208	switch (cputype) {
209#if	defined(__i386__) || defined(__amd64__)
210		/*
211		 * Intel Core, Core 2 and Atom processors.
212		 */
213	case PMC_CPU_INTEL_ATOM:
214	case PMC_CPU_INTEL_ATOM_SILVERMONT:
215	case PMC_CPU_INTEL_CORE:
216	case PMC_CPU_INTEL_CORE2:
217	case PMC_CPU_INTEL_CORE2EXTREME:
218	case PMC_CPU_INTEL_COREI7:
219	case PMC_CPU_INTEL_NEHALEM_EX:
220	case PMC_CPU_INTEL_IVYBRIDGE:
221	case PMC_CPU_INTEL_SANDYBRIDGE:
222	case PMC_CPU_INTEL_WESTMERE:
223	case PMC_CPU_INTEL_WESTMERE_EX:
224	case PMC_CPU_INTEL_SANDYBRIDGE_XEON:
225	case PMC_CPU_INTEL_IVYBRIDGE_XEON:
226	case PMC_CPU_INTEL_HASWELL:
227		error = pmc_core_initialize(pmc_mdep, ncpus);
228		break;
229
230		/*
231		 * Intel Pentium 4 Processors, and P4/EMT64 processors.
232		 */
233
234	case PMC_CPU_INTEL_PIV:
235		error = pmc_p4_initialize(pmc_mdep, ncpus);
236		break;
237#endif
238
239#if	defined(__i386__)
240		/*
241		 * P6 Family Processors
242		 */
243
244	case PMC_CPU_INTEL_P6:
245	case PMC_CPU_INTEL_CL:
246	case PMC_CPU_INTEL_PII:
247	case PMC_CPU_INTEL_PIII:
248	case PMC_CPU_INTEL_PM:
249		error = pmc_p6_initialize(pmc_mdep, ncpus);
250		break;
251
252		/*
253		 * Intel Pentium PMCs.
254		 */
255
256	case PMC_CPU_INTEL_P5:
257		error = pmc_p5_initialize(pmc_mdep, ncpus);
258		break;
259#endif
260
261	default:
262		KASSERT(0, ("[intel,%d] Unknown CPU type", __LINE__));
263	}
264
265	if (error) {
266		pmc_tsc_finalize(pmc_mdep);
267		goto error;
268	}
269
270	/*
271	 * Init the uncore class.
272	 */
273#if	defined(__i386__) || defined(__amd64__)
274	switch (cputype) {
275		/*
276		 * Intel Corei7 and Westmere processors.
277		 */
278	case PMC_CPU_INTEL_COREI7:
279	case PMC_CPU_INTEL_HASWELL:
280	case PMC_CPU_INTEL_SANDYBRIDGE:
281	case PMC_CPU_INTEL_WESTMERE:
282		error = pmc_uncore_initialize(pmc_mdep, ncpus);
283		break;
284	default:
285		break;
286	}
287#endif
288  error:
289	if (error) {
290		pmc_mdep_free(pmc_mdep);
291		pmc_mdep = NULL;
292	}
293
294	return (pmc_mdep);
295}
296
297void
298pmc_intel_finalize(struct pmc_mdep *md)
299{
300	pmc_tsc_finalize(md);
301
302	switch (md->pmd_cputype) {
303#if	defined(__i386__) || defined(__amd64__)
304	case PMC_CPU_INTEL_ATOM:
305	case PMC_CPU_INTEL_ATOM_SILVERMONT:
306	case PMC_CPU_INTEL_CORE:
307	case PMC_CPU_INTEL_CORE2:
308	case PMC_CPU_INTEL_CORE2EXTREME:
309	case PMC_CPU_INTEL_COREI7:
310	case PMC_CPU_INTEL_NEHALEM_EX:
311	case PMC_CPU_INTEL_HASWELL:
312	case PMC_CPU_INTEL_IVYBRIDGE:
313	case PMC_CPU_INTEL_SANDYBRIDGE:
314	case PMC_CPU_INTEL_WESTMERE:
315	case PMC_CPU_INTEL_WESTMERE_EX:
316	case PMC_CPU_INTEL_SANDYBRIDGE_XEON:
317	case PMC_CPU_INTEL_IVYBRIDGE_XEON:
318		pmc_core_finalize(md);
319		break;
320
321	case PMC_CPU_INTEL_PIV:
322		pmc_p4_finalize(md);
323		break;
324#endif
325#if	defined(__i386__)
326	case PMC_CPU_INTEL_P6:
327	case PMC_CPU_INTEL_CL:
328	case PMC_CPU_INTEL_PII:
329	case PMC_CPU_INTEL_PIII:
330	case PMC_CPU_INTEL_PM:
331		pmc_p6_finalize(md);
332		break;
333	case PMC_CPU_INTEL_P5:
334		pmc_p5_finalize(md);
335		break;
336#endif
337	default:
338		KASSERT(0, ("[intel,%d] unknown CPU type", __LINE__));
339	}
340
341	/*
342	 * Uncore.
343	 */
344#if	defined(__i386__) || defined(__amd64__)
345	switch (md->pmd_cputype) {
346	case PMC_CPU_INTEL_COREI7:
347	case PMC_CPU_INTEL_HASWELL:
348	case PMC_CPU_INTEL_SANDYBRIDGE:
349	case PMC_CPU_INTEL_WESTMERE:
350		pmc_uncore_finalize(md);
351		break;
352	default:
353		break;
354	}
355#endif
356}
357