hwpmc_intel.c revision 233628
1277323Sdim/*-
2277323Sdim * Copyright (c) 2008 Joseph Koshy
3353358Sdim * All rights reserved.
4353358Sdim *
5353358Sdim * Redistribution and use in source and binary forms, with or without
6277323Sdim * modification, are permitted provided that the following conditions
7277323Sdim * are met:
8277323Sdim * 1. Redistributions of source code must retain the above copyright
9277323Sdim *    notice, this list of conditions and the following disclaimer.
10277323Sdim * 2. Redistributions in binary form must reproduce the above copyright
11277323Sdim *    notice, this list of conditions and the following disclaimer in the
12277323Sdim *    documentation and/or other materials provided with the distribution.
13277323Sdim *
14277323Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15277323Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16277323Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17341825Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18277323Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19277323Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20288943Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21277323Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22277323Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23277323Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24277323Sdim * SUCH DAMAGE.
25277323Sdim */
26277323Sdim
27277323Sdim/*
28277323Sdim * Common code for handling Intel CPUs.
29277323Sdim */
30277323Sdim
31277323Sdim#include <sys/cdefs.h>
32277323Sdim__FBSDID("$FreeBSD: head/sys/dev/hwpmc/hwpmc_intel.c 233628 2012-03-28 20:58:30Z fabient $");
33277323Sdim
34277323Sdim#include <sys/param.h>
35277323Sdim#include <sys/pmc.h>
36277323Sdim#include <sys/pmckern.h>
37277323Sdim#include <sys/systm.h>
38277323Sdim
39277323Sdim#include <machine/cpu.h>
40277323Sdim#include <machine/cputypes.h>
41277323Sdim#include <machine/md_var.h>
42277323Sdim#include <machine/specialreg.h>
43277323Sdim
44277323Sdimstatic int
45277323Sdimintel_switch_in(struct pmc_cpu *pc, struct pmc_process *pp)
46277323Sdim{
47341825Sdim	(void) pc;
48277323Sdim
49277323Sdim	PMCDBG(MDP,SWI,1, "pc=%p pp=%p enable-msr=%d", pc, pp,
50277323Sdim	    pp->pp_flags & PMC_PP_ENABLE_MSR_ACCESS);
51277323Sdim
52277323Sdim	/* allow the RDPMC instruction if needed */
53277323Sdim	if (pp->pp_flags & PMC_PP_ENABLE_MSR_ACCESS)
54277323Sdim		load_cr4(rcr4() | CR4_PCE);
55277323Sdim
56277323Sdim	PMCDBG(MDP,SWI,1, "cr4=0x%jx", (uintmax_t) rcr4());
57277323Sdim
58309124Sdim	return 0;
59309124Sdim}
60277323Sdim
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
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:	/* 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 */
136		case 0x2E:
137			cputype = PMC_CPU_INTEL_COREI7;
138			nclasses = 5;
139			break;
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;
143			nclasses = 5;
144			break;
145		case 0x2A:	/* Per Intel document 253669-039US 05/2011. */
146		case 0x2D:	/* Per Intel document 253669-041US 12/2011. */
147			cputype = PMC_CPU_INTEL_SANDYBRIDGE;
148			nclasses = 5;
149			break;
150		}
151		break;
152#if	defined(__i386__) || defined(__amd64__)
153	case 0xF00:		/* P4 */
154		if (model >= 0 && model <= 6) /* known models */
155			cputype = PMC_CPU_INTEL_PIV;
156		break;
157	}
158#endif
159
160	if ((int) cputype == -1) {
161		printf("pmc: Unknown Intel CPU.\n");
162		return (NULL);
163	}
164
165	/* Allocate base class and initialize machine dependent struct */
166	pmc_mdep = pmc_mdep_alloc(nclasses);
167
168	pmc_mdep->pmd_cputype 	 = cputype;
169	pmc_mdep->pmd_switch_in	 = intel_switch_in;
170	pmc_mdep->pmd_switch_out = intel_switch_out;
171
172	ncpus = pmc_cpu_max();
173
174	error = pmc_tsc_initialize(pmc_mdep, ncpus);
175	if (error)
176		goto error;
177
178	switch (cputype) {
179#if	defined(__i386__) || defined(__amd64__)
180		/*
181		 * Intel Core, Core 2 and Atom processors.
182		 */
183	case PMC_CPU_INTEL_ATOM:
184	case PMC_CPU_INTEL_CORE:
185	case PMC_CPU_INTEL_CORE2:
186	case PMC_CPU_INTEL_CORE2EXTREME:
187	case PMC_CPU_INTEL_COREI7:
188	case PMC_CPU_INTEL_SANDYBRIDGE:
189	case PMC_CPU_INTEL_WESTMERE:
190		error = pmc_core_initialize(pmc_mdep, ncpus);
191		break;
192
193		/*
194		 * Intel Pentium 4 Processors, and P4/EMT64 processors.
195		 */
196
197	case PMC_CPU_INTEL_PIV:
198		error = pmc_p4_initialize(pmc_mdep, ncpus);
199
200		KASSERT(pmc_mdep->pmd_npmc == TSC_NPMCS + P4_NPMCS,
201		    ("[intel,%d] incorrect npmc count %d", __LINE__,
202		    pmc_mdep->pmd_npmc));
203		break;
204#endif
205
206#if	defined(__i386__)
207		/*
208		 * P6 Family Processors
209		 */
210
211	case PMC_CPU_INTEL_P6:
212	case PMC_CPU_INTEL_CL:
213	case PMC_CPU_INTEL_PII:
214	case PMC_CPU_INTEL_PIII:
215	case PMC_CPU_INTEL_PM:
216		error = pmc_p6_initialize(pmc_mdep, ncpus);
217
218		KASSERT(pmc_mdep->pmd_npmc == TSC_NPMCS + P6_NPMCS,
219		    ("[intel,%d] incorrect npmc count %d", __LINE__,
220		    pmc_mdep->pmd_npmc));
221		break;
222
223		/*
224		 * Intel Pentium PMCs.
225		 */
226
227	case PMC_CPU_INTEL_P5:
228		error = pmc_p5_initialize(pmc_mdep, ncpus);
229
230		KASSERT(pmc_mdep->pmd_npmc == TSC_NPMCS + PENTIUM_NPMCS,
231		    ("[intel,%d] incorrect npmc count %d", __LINE__,
232		    pmc_mdep->pmd_npmc));
233		break;
234#endif
235
236	default:
237		KASSERT(0, ("[intel,%d] Unknown CPU type", __LINE__));
238	}
239
240	if (error)
241		goto error;
242
243	/*
244	 * Init the uncore class.
245	 */
246#if	defined(__i386__) || defined(__amd64__)
247	switch (cputype) {
248		/*
249		 * Intel Corei7 and Westmere processors.
250		 */
251	case PMC_CPU_INTEL_COREI7:
252	case PMC_CPU_INTEL_SANDYBRIDGE:
253	case PMC_CPU_INTEL_WESTMERE:
254		error = pmc_uncore_initialize(pmc_mdep, ncpus);
255		break;
256	default:
257		break;
258	}
259#endif
260
261  error:
262	if (error) {
263		free(pmc_mdep, M_PMC);
264		pmc_mdep = NULL;
265	}
266
267	return (pmc_mdep);
268}
269
270void
271pmc_intel_finalize(struct pmc_mdep *md)
272{
273	pmc_tsc_finalize(md);
274
275	switch (md->pmd_cputype) {
276#if	defined(__i386__) || defined(__amd64__)
277	case PMC_CPU_INTEL_ATOM:
278	case PMC_CPU_INTEL_CORE:
279	case PMC_CPU_INTEL_CORE2:
280	case PMC_CPU_INTEL_CORE2EXTREME:
281	case PMC_CPU_INTEL_COREI7:
282	case PMC_CPU_INTEL_SANDYBRIDGE:
283	case PMC_CPU_INTEL_WESTMERE:
284		pmc_core_finalize(md);
285		break;
286
287	case PMC_CPU_INTEL_PIV:
288		pmc_p4_finalize(md);
289		break;
290#endif
291#if	defined(__i386__)
292	case PMC_CPU_INTEL_P6:
293	case PMC_CPU_INTEL_CL:
294	case PMC_CPU_INTEL_PII:
295	case PMC_CPU_INTEL_PIII:
296	case PMC_CPU_INTEL_PM:
297		pmc_p6_finalize(md);
298		break;
299	case PMC_CPU_INTEL_P5:
300		pmc_p5_finalize(md);
301		break;
302#endif
303	default:
304		KASSERT(0, ("[intel,%d] unknown CPU type", __LINE__));
305	}
306
307	/*
308	 * Uncore.
309	 */
310#if	defined(__i386__) || defined(__amd64__)
311	switch (md->pmd_cputype) {
312	case PMC_CPU_INTEL_COREI7:
313	case PMC_CPU_INTEL_SANDYBRIDGE:
314	case PMC_CPU_INTEL_WESTMERE:
315		pmc_uncore_finalize(md);
316		break;
317	default:
318		break;
319	}
320#endif
321}
322