3909 "zfs send -D" does not work
[illumos-gate.git] / usr / src / uts / i86pc / os / cpupm / turbo.c
blob7844ce224748e5178715f8c70434c160e62d2413
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2009, Intel Corporation.
26 * All Rights Reserved.
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>
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;
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 *);
53 static kmutex_t turbo_mutex;
55 turbo_kstat_t turbo_kstat = {
56 { "turbo_supported", KSTAT_DATA_UINT32 },
57 { "turbo_mcnt", KSTAT_DATA_UINT64 },
58 { "turbo_acnt", KSTAT_DATA_UINT64 },
61 #define CPU_ACPI_P0 0
62 #define CPU_IN_TURBO 1
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
69 #define IA32_MPERF_MSR 0xE7
70 #define IA32_APERF_MSR 0xE8
73 * kstat update function of the turbo mode info
75 static int
76 turbo_kstat_update(kstat_t *ksp, int flag)
78 cpupm_mach_turbo_info_t *turbo_info = ksp->ks_private;
80 if (flag == KSTAT_WRITE) {
81 return (EACCES);
85 * update the count in case CPU is in the turbo
86 * mode for a long time
88 if (turbo_info->in_turbo == CPU_IN_TURBO)
89 update_turbo_info(turbo_info);
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;
96 return (0);
100 * update the sum of counts and clear MSRs
102 static void
103 update_turbo_info(cpupm_mach_turbo_info_t *turbo_info)
105 ulong_t iflag;
106 uint64_t mcnt, acnt;
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);
119 * Get count of MPERF/APERF MSR
121 static void
122 get_turbo_info(cpupm_mach_turbo_info_t *turbo_info)
124 ulong_t iflag;
125 uint64_t mcnt, acnt;
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);
136 * Clear MPERF/APERF MSR
138 static void
139 reset_turbo_info(void)
141 ulong_t iflag;
143 iflag = intr_clear();
144 wrmsr(IA32_MPERF_MSR, 0);
145 wrmsr(IA32_APERF_MSR, 0);
146 intr_restore(iflag);
150 * sum up the count of one CPU_ACPI_P0 transition
152 void
153 cpupm_record_turbo_info(cpupm_mach_turbo_info_t *turbo_info,
154 uint32_t cur_state, uint32_t req_state)
156 if (!turbo_info->turbo_supported)
157 return;
159 * enter P0 state
161 if (req_state == CPU_ACPI_P0) {
162 reset_turbo_info();
163 turbo_info->in_turbo = CPU_IN_TURBO;
166 * Leave P0 state
168 else if (cur_state == CPU_ACPI_P0) {
169 turbo_info->in_turbo = 0;
170 get_turbo_info(turbo_info);
174 cpupm_mach_turbo_info_t *
175 cpupm_turbo_init(cpu_t *cp)
177 cpupm_mach_turbo_info_t *turbo_info;
179 turbo_info = kmem_zalloc(sizeof (cpupm_mach_turbo_info_t), KM_SLEEP);
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);
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;
196 kstat_install(turbo_info->turbo_ksp);
199 return (turbo_info);
202 void
203 cpupm_turbo_fini(cpupm_mach_turbo_info_t *turbo_info)
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));