xref: /illumos-gate/usr/src/uts/i86pc/os/cpupm/turbo.c (revision 4f364e7c95ee7fd9d5bbeddc1940e92405bb0e72)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 /*
25  * Copyright (c) 2009,  Intel Corporation.
26  * All Rights Reserved.
27  */
28 
29 #include <sys/x86_archext.h>
30 #include <sys/machsystm.h>
31 #include <sys/archsystm.h>
32 #include <sys/x_call.h>
33 #include <sys/acpi/acpi.h>
34 #include <sys/acpica.h>
35 #include <sys/speedstep.h>
36 #include <sys/cpu_acpi.h>
37 #include <sys/cpupm.h>
38 #include <sys/dtrace.h>
39 #include <sys/sdt.h>
40 
41 typedef struct turbo_kstat_s {
42 	struct kstat_named	turbo_supported;	/* turbo flag */
43 	struct kstat_named	t_mcnt;			/* IA32_MPERF_MSR */
44 	struct kstat_named	t_acnt;			/* IA32_APERF_MSR */
45 } turbo_kstat_t;
46 
47 static int turbo_kstat_update(kstat_t *, int);
48 static void get_turbo_info(cpupm_mach_turbo_info_t *);
49 static void reset_turbo_info(void);
50 static void record_turbo_info(cpupm_mach_turbo_info_t *, uint32_t, uint32_t);
51 static void update_turbo_info(cpupm_mach_turbo_info_t *);
52 
53 static kmutex_t turbo_mutex;
54 
55 turbo_kstat_t turbo_kstat = {
56 	{ "turbo_supported",	KSTAT_DATA_UINT32 },
57 	{ "turbo_mcnt",		KSTAT_DATA_UINT64 },
58 	{ "turbo_acnt",		KSTAT_DATA_UINT64 },
59 };
60 
61 #define	CPU_ACPI_P0			0
62 #define	CPU_IN_TURBO			1
63 
64 /*
65  * MSR for hardware coordination feedback mechanism
66  *   - IA32_MPERF: increments in proportion to a fixed frequency
67  *   - IA32_APERF: increments in proportion to actual performance
68  */
69 #define	IA32_MPERF_MSR			0xE7
70 #define	IA32_APERF_MSR			0xE8
71 
72 /*
73  * kstat update function of the turbo mode info
74  */
75 static int
76 turbo_kstat_update(kstat_t *ksp, int flag)
77 {
78 	cpupm_mach_turbo_info_t *turbo_info = ksp->ks_private;
79 
80 	if (flag == KSTAT_WRITE) {
81 		return (EACCES);
82 	}
83 
84 	/*
85 	 * update the count in case CPU is in the turbo
86 	 * mode for a long time
87 	 */
88 	if (turbo_info->in_turbo == CPU_IN_TURBO)
89 		update_turbo_info(turbo_info);
90 
91 	turbo_kstat.turbo_supported.value.ui32 =
92 	    turbo_info->turbo_supported;
93 	turbo_kstat.t_mcnt.value.ui64 = turbo_info->t_mcnt;
94 	turbo_kstat.t_acnt.value.ui64 = turbo_info->t_acnt;
95 
96 	return (0);
97 }
98 
99 /*
100  * update the sum of counts and clear MSRs
101  */
102 static void
103 update_turbo_info(cpupm_mach_turbo_info_t *turbo_info)
104 {
105 	ulong_t		iflag;
106 	uint64_t	mcnt, acnt;
107 
108 	iflag = intr_clear();
109 	mcnt = rdmsr(IA32_MPERF_MSR);
110 	acnt = rdmsr(IA32_APERF_MSR);
111 	wrmsr(IA32_MPERF_MSR, 0);
112 	wrmsr(IA32_APERF_MSR, 0);
113 	turbo_info->t_mcnt += mcnt;
114 	turbo_info->t_acnt += acnt;
115 	intr_restore(iflag);
116 }
117 
118 /*
119  * Get count of MPERF/APERF MSR
120  */
121 static void
122 get_turbo_info(cpupm_mach_turbo_info_t *turbo_info)
123 {
124 	ulong_t		iflag;
125 	uint64_t	mcnt, acnt;
126 
127 	iflag = intr_clear();
128 	mcnt = rdmsr(IA32_MPERF_MSR);
129 	acnt = rdmsr(IA32_APERF_MSR);
130 	turbo_info->t_mcnt += mcnt;
131 	turbo_info->t_acnt += acnt;
132 	intr_restore(iflag);
133 }
134 
135 /*
136  * Clear MPERF/APERF MSR
137  */
138 static void
139 reset_turbo_info(void)
140 {
141 	ulong_t		iflag;
142 
143 	iflag = intr_clear();
144 	wrmsr(IA32_MPERF_MSR, 0);
145 	wrmsr(IA32_APERF_MSR, 0);
146 	intr_restore(iflag);
147 }
148 
149 /*
150  * sum up the count of one CPU_ACPI_P0 transition
151  */
152 void
153 cpupm_record_turbo_info(cpupm_mach_turbo_info_t *turbo_info,
154     uint32_t cur_state, uint32_t req_state)
155 {
156 	if (!turbo_info->turbo_supported)
157 		return;
158 	/*
159 	 * enter P0 state
160 	 */
161 	if (req_state == CPU_ACPI_P0) {
162 		reset_turbo_info();
163 		turbo_info->in_turbo = CPU_IN_TURBO;
164 	}
165 	/*
166 	 * Leave P0 state
167 	 */
168 	else if (cur_state == CPU_ACPI_P0) {
169 		turbo_info->in_turbo = 0;
170 		get_turbo_info(turbo_info);
171 	}
172 }
173 
174 cpupm_mach_turbo_info_t *
175 cpupm_turbo_init(cpu_t *cp)
176 {
177 	cpupm_mach_turbo_info_t *turbo_info;
178 
179 	turbo_info = kmem_zalloc(sizeof (cpupm_mach_turbo_info_t), KM_SLEEP);
180 
181 	turbo_info->turbo_supported = 1;
182 	turbo_info->turbo_ksp = kstat_create("turbo", cp->cpu_id,
183 	    "turbo", "misc", KSTAT_TYPE_NAMED,
184 	    sizeof (turbo_kstat) / sizeof (kstat_named_t),
185 	    KSTAT_FLAG_VIRTUAL);
186 
187 	if (turbo_info->turbo_ksp == NULL) {
188 		cmn_err(CE_NOTE, "kstat_create(turbo) fail");
189 	} else {
190 		turbo_info->turbo_ksp->ks_data = &turbo_kstat;
191 		turbo_info->turbo_ksp->ks_lock = &turbo_mutex;
192 		turbo_info->turbo_ksp->ks_update = turbo_kstat_update;
193 		turbo_info->turbo_ksp->ks_data_size += MAXNAMELEN;
194 		turbo_info->turbo_ksp->ks_private = turbo_info;
195 
196 		kstat_install(turbo_info->turbo_ksp);
197 	}
198 
199 	return (turbo_info);
200 }
201 
202 void
203 cpupm_turbo_fini(cpupm_mach_turbo_info_t *turbo_info)
204 {
205 	if (turbo_info->turbo_ksp != NULL)
206 		kstat_delete(turbo_info->turbo_ksp);
207 	kmem_free(turbo_info, sizeof (cpupm_mach_turbo_info_t));
208 }
209