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 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/x86_archext.h> 27 #include <sys/machsystm.h> 28 #include <sys/x_call.h> 29 #include <sys/cpu_acpi.h> 30 #include <sys/cpupm_throttle.h> 31 #include <sys/dtrace.h> 32 #include <sys/sdt.h> 33 34 static int cpupm_throttle_init(cpu_t *); 35 static void cpupm_throttle_fini(cpu_t *); 36 static void cpupm_throttle(cpuset_t, uint32_t); 37 38 cpupm_state_ops_t cpupm_throttle_ops = { 39 "Generic ACPI T-state Support", 40 cpupm_throttle_init, 41 cpupm_throttle_fini, 42 cpupm_throttle 43 }; 44 45 /* 46 * Error returns 47 */ 48 #define THROTTLE_RET_SUCCESS 0x00 49 #define THROTTLE_RET_INCOMPLETE_DATA 0x01 50 #define THROTTLE_RET_UNSUP_STATE 0x02 51 #define THROTTLE_RET_TRANS_INCOMPLETE 0x03 52 53 #define THROTTLE_LATENCY_WAIT 1 54 55 /* 56 * MSR register for clock modulation 57 */ 58 #define IA32_CLOCK_MODULATION_MSR 0x19A 59 60 /* 61 * Debugging support 62 */ 63 #ifdef DEBUG 64 volatile int cpupm_throttle_debug = 0; 65 #define CTDEBUG(arglist) if (cpupm_throttle_debug) printf arglist; 66 #else 67 #define CTDEBUG(arglist) 68 #endif 69 70 /* 71 * Write the _PTC ctrl register. How it is written, depends upon the _PTC 72 * APCI object value. 73 */ 74 static int 75 write_ctrl(cpu_acpi_handle_t handle, uint32_t ctrl) 76 { 77 cpu_acpi_ptc_t *ptc_ctrl; 78 uint64_t reg; 79 int ret = 0; 80 81 ptc_ctrl = CPU_ACPI_PTC_CTRL(handle); 82 83 switch (ptc_ctrl->cr_addrspace_id) { 84 case ACPI_ADR_SPACE_FIXED_HARDWARE: 85 /* 86 * Read current thermal state because reserved bits must be 87 * preserved, compose new value, and write it.The writable 88 * bits are 4:1 (1 to 4). 89 * Bits 3:1 => On-Demand Clock Modulation Duty Cycle 90 * Bit 4 => On-Demand Clock Modulation Enable 91 * Left shift ctrl by 1 to allign with bits 1-4 of MSR 92 */ 93 reg = rdmsr(IA32_CLOCK_MODULATION_MSR); 94 reg &= ~((uint64_t)0x1E); 95 reg |= ctrl; 96 wrmsr(IA32_CLOCK_MODULATION_MSR, reg); 97 break; 98 99 case ACPI_ADR_SPACE_SYSTEM_IO: 100 ret = cpu_acpi_write_port(ptc_ctrl->cr_address, ctrl, 101 ptc_ctrl->cr_width); 102 break; 103 104 default: 105 DTRACE_PROBE1(throttle_ctrl_unsupported_type, uint8_t, 106 ptc_ctrl->cr_addrspace_id); 107 108 ret = -1; 109 } 110 111 DTRACE_PROBE1(throttle_ctrl_write, uint32_t, ctrl); 112 DTRACE_PROBE1(throttle_ctrl_write_err, int, ret); 113 114 return (ret); 115 } 116 117 static int 118 read_status(cpu_acpi_handle_t handle, uint32_t *stat) 119 { 120 cpu_acpi_ptc_t *ptc_stat; 121 uint64_t reg; 122 int ret = 0; 123 124 ptc_stat = CPU_ACPI_PTC_STATUS(handle); 125 126 switch (ptc_stat->cr_addrspace_id) { 127 case ACPI_ADR_SPACE_FIXED_HARDWARE: 128 reg = rdmsr(IA32_CLOCK_MODULATION_MSR); 129 *stat = reg & 0x1E; 130 ret = 0; 131 break; 132 133 case ACPI_ADR_SPACE_SYSTEM_IO: 134 ret = cpu_acpi_read_port(ptc_stat->cr_address, stat, 135 ptc_stat->cr_width); 136 break; 137 138 default: 139 DTRACE_PROBE1(throttle_status_unsupported_type, uint8_t, 140 ptc_stat->cr_addrspace_id); 141 142 return (-1); 143 } 144 145 DTRACE_PROBE1(throttle_status_read, uint32_t, *stat); 146 DTRACE_PROBE1(throttle_status_read_err, int, ret); 147 148 return (ret); 149 } 150 151 /* 152 * Transition the current processor to the requested throttling state. 153 */ 154 static void 155 cpupm_tstate_transition(uint32_t req_state) 156 { 157 cpupm_mach_state_t *mach_state = 158 (cpupm_mach_state_t *)CPU->cpu_m.mcpu_pm_mach_state; 159 cpu_acpi_handle_t handle = mach_state->ms_acpi_handle; 160 cpu_acpi_tstate_t *req_tstate; 161 uint32_t ctrl; 162 uint32_t stat; 163 int i; 164 165 req_tstate = (cpu_acpi_tstate_t *)CPU_ACPI_TSTATES(handle); 166 req_tstate += req_state; 167 DTRACE_PROBE1(throttle_transition, uint32_t, 168 CPU_ACPI_FREQPER(req_tstate)); 169 170 /* 171 * Initiate the processor t-state change. 172 */ 173 ctrl = CPU_ACPI_TSTATE_CTRL(req_tstate); 174 if (write_ctrl(handle, ctrl) != 0) { 175 return; 176 } 177 178 /* 179 * If status is zero, then transition is synchronous and 180 * no status value comparison is required. 181 */ 182 if (CPU_ACPI_TSTATE_STAT(req_tstate) == 0) { 183 return; 184 } 185 186 /* Wait until switch is complete, but bound the loop just in case. */ 187 for (i = CPU_ACPI_TSTATE_TRANSLAT(req_tstate) * 2; i >= 0; 188 i -= THROTTLE_LATENCY_WAIT) { 189 if (read_status(handle, &stat) == 0 && 190 CPU_ACPI_TSTATE_STAT(req_tstate) == stat) 191 break; 192 drv_usecwait(THROTTLE_LATENCY_WAIT); 193 } 194 195 if (CPU_ACPI_TSTATE_STAT(req_tstate) != stat) { 196 DTRACE_PROBE(throttle_transition_incomplete); 197 } 198 } 199 200 static void 201 cpupm_throttle(cpuset_t set, uint32_t throtl_lvl) 202 { 203 /* 204 * If thread is already running on target CPU then just 205 * make the transition request. Otherwise, we'll need to 206 * make a cross-call. 207 */ 208 kpreempt_disable(); 209 if (CPU_IN_SET(set, CPU->cpu_id)) { 210 cpupm_tstate_transition(throtl_lvl); 211 CPUSET_DEL(set, CPU->cpu_id); 212 } 213 if (!CPUSET_ISNULL(set)) { 214 xc_call((xc_arg_t)throtl_lvl, NULL, NULL, 215 CPUSET2BV(set), (xc_func_t)cpupm_tstate_transition); 216 } 217 kpreempt_enable(); 218 } 219 220 static int 221 cpupm_throttle_init(cpu_t *cp) 222 { 223 cpupm_mach_state_t *mach_state = 224 (cpupm_mach_state_t *)cp->cpu_m.mcpu_pm_mach_state; 225 cpu_acpi_handle_t handle = mach_state->ms_acpi_handle; 226 cpu_acpi_ptc_t *ptc_stat; 227 int ret; 228 229 if ((ret = cpu_acpi_cache_tstate_data(handle)) != 0) { 230 if (ret < 0) 231 cmn_err(CE_NOTE, 232 "!Support for CPU throttling is being " 233 "disabled due to errors parsing ACPI T-state " 234 "objects exported by BIOS."); 235 cpupm_throttle_fini(cp); 236 return (THROTTLE_RET_INCOMPLETE_DATA); 237 } 238 239 /* 240 * Check the address space used for transitions 241 */ 242 ptc_stat = CPU_ACPI_PTC_STATUS(handle); 243 switch (ptc_stat->cr_addrspace_id) { 244 case ACPI_ADR_SPACE_FIXED_HARDWARE: 245 CTDEBUG(("T-State transitions will use fixed hardware\n")); 246 break; 247 case ACPI_ADR_SPACE_SYSTEM_IO: 248 CTDEBUG(("T-State transitions will use System IO\n")); 249 break; 250 default: 251 cmn_err(CE_NOTE, "!_PTC configured for unsupported " 252 "address space type = %d.", ptc_stat->cr_addrspace_id); 253 return (THROTTLE_RET_INCOMPLETE_DATA); 254 } 255 256 cpupm_alloc_domains(cp, CPUPM_T_STATES); 257 258 return (THROTTLE_RET_SUCCESS); 259 } 260 261 static void 262 cpupm_throttle_fini(cpu_t *cp) 263 { 264 cpupm_mach_state_t *mach_state = 265 (cpupm_mach_state_t *)cp->cpu_m.mcpu_pm_mach_state; 266 cpu_acpi_handle_t handle = mach_state->ms_acpi_handle; 267 268 cpupm_free_domains(&cpupm_tstate_domains); 269 cpu_acpi_free_tstate_data(handle); 270 } 271 272 /* 273 * This routine reads the ACPI _TPC object. It's accessed as a callback 274 * by the cpu driver whenever a _TPC change notification is received. 275 */ 276 static int 277 cpupm_throttle_get_max(processorid_t cpu_id) 278 { 279 cpu_t *cp = cpu[cpu_id]; 280 cpupm_mach_state_t *mach_state = 281 (cpupm_mach_state_t *)(cp->cpu_m.mcpu_pm_mach_state); 282 cpu_acpi_handle_t handle; 283 int throtl_level; 284 int max_throttle_lvl; 285 uint_t num_throtl; 286 287 if (mach_state == NULL) { 288 return (-1); 289 } 290 291 handle = mach_state->ms_acpi_handle; 292 ASSERT(handle != NULL); 293 294 cpu_acpi_cache_tpc(handle); 295 throtl_level = CPU_ACPI_TPC(handle); 296 297 num_throtl = CPU_ACPI_TSTATES_COUNT(handle); 298 299 max_throttle_lvl = num_throtl - 1; 300 if ((throtl_level < 0) || (throtl_level > max_throttle_lvl)) { 301 cmn_err(CE_NOTE, "!cpupm_throttle_get_max: CPU %d: " 302 "_TPC out of range %d", cp->cpu_id, throtl_level); 303 throtl_level = 0; 304 } 305 306 return (throtl_level); 307 } 308 309 /* 310 * Take care of CPU throttling when _TPC notification arrives 311 */ 312 void 313 cpupm_throttle_manage_notification(void *ctx) 314 { 315 cpu_t *cp = ctx; 316 processorid_t cpu_id = cp->cpu_id; 317 cpupm_mach_state_t *mach_state = 318 (cpupm_mach_state_t *)cp->cpu_m.mcpu_pm_mach_state; 319 boolean_t is_ready; 320 int new_level; 321 322 if (mach_state == NULL) { 323 return; 324 } 325 326 /* 327 * We currently refuse to power-manage if the CPU is not ready to 328 * take cross calls (cross calls fail silently if CPU is not ready 329 * for it). 330 * 331 * Additionally, for x86 platforms we cannot power-manage 332 * any one instance, until all instances have been initialized. 333 * That's because we don't know what the CPU domains look like 334 * until all instances have been initialized. 335 */ 336 is_ready = (cp->cpu_flags & CPU_READY) && cpupm_throttle_ready(); 337 if (!is_ready) 338 return; 339 340 if (!(mach_state->ms_caps & CPUPM_T_STATES)) 341 return; 342 ASSERT(mach_state->ms_tstate.cma_ops != NULL); 343 344 /* 345 * Get the new T-State support level 346 */ 347 new_level = cpupm_throttle_get_max(cpu_id); 348 349 cpupm_state_change(cp, new_level, CPUPM_T_STATES); 350 } 351