1fec754d4SMike Smith /*- 2fec754d4SMike Smith * Copyright (c) 2001 Michael Smith 3fec754d4SMike Smith * All rights reserved. 4fec754d4SMike Smith * 5fec754d4SMike Smith * Redistribution and use in source and binary forms, with or without 6fec754d4SMike Smith * modification, are permitted provided that the following conditions 7fec754d4SMike Smith * are met: 8fec754d4SMike Smith * 1. Redistributions of source code must retain the above copyright 9fec754d4SMike Smith * notice, this list of conditions and the following disclaimer. 10fec754d4SMike Smith * 2. Redistributions in binary form must reproduce the above copyright 11fec754d4SMike Smith * notice, this list of conditions and the following disclaimer in the 12fec754d4SMike Smith * documentation and/or other materials provided with the distribution. 13fec754d4SMike Smith * 14fec754d4SMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15fec754d4SMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16fec754d4SMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17fec754d4SMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18fec754d4SMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19fec754d4SMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20fec754d4SMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21fec754d4SMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22fec754d4SMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23fec754d4SMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24fec754d4SMike Smith * SUCH DAMAGE. 25fec754d4SMike Smith * 26fec754d4SMike Smith * $FreeBSD$ 27fec754d4SMike Smith */ 28fec754d4SMike Smith 29fec754d4SMike Smith #include "opt_acpi.h" 30fec754d4SMike Smith #include <sys/param.h> 31fec754d4SMike Smith #include <sys/kernel.h> 32fec754d4SMike Smith #include <sys/bus.h> 33899ccf54SMitsuru IWASAKI #include <sys/power.h> 34fec754d4SMike Smith 35fec754d4SMike Smith #include <machine/bus_pio.h> 36fec754d4SMike Smith #include <machine/bus.h> 37fec754d4SMike Smith #include <machine/resource.h> 38fec754d4SMike Smith #include <sys/rman.h> 39fec754d4SMike Smith 40fec754d4SMike Smith #include "acpi.h" 41fec754d4SMike Smith 42fec754d4SMike Smith #include <dev/acpica/acpivar.h> 43fec754d4SMike Smith 44fec754d4SMike Smith /* 45fec754d4SMike Smith * Support for ACPI Processor devices. 46fec754d4SMike Smith * 47fec754d4SMike Smith * Note that this only provides ACPI 1.0 support (with the exception of the 48fec754d4SMike Smith * PSTATE_CNT field). 2.0 support will involve implementing _PTC, _PCT, 49fec754d4SMike Smith * _PSS and _PPC. 50fec754d4SMike Smith */ 51fec754d4SMike Smith 52fec754d4SMike Smith /* 53fec754d4SMike Smith * Hooks for the ACPI CA debugging infrastructure 54fec754d4SMike Smith */ 55fec754d4SMike Smith #define _COMPONENT ACPI_PROCESSOR 569127281cSMike Smith ACPI_MODULE_NAME("PROCESSOR") 57fec754d4SMike Smith 58fec754d4SMike Smith struct acpi_cpu_softc { 59fec754d4SMike Smith device_t cpu_dev; 60fec754d4SMike Smith ACPI_HANDLE cpu_handle; 61fec754d4SMike Smith 62fec754d4SMike Smith u_int32_t cpu_id; 63fec754d4SMike Smith 64fec754d4SMike Smith /* CPU throttling control register */ 65fec754d4SMike Smith struct resource *cpu_p_blk; 66fec754d4SMike Smith #define CPU_GET_P_CNT(sc) (bus_space_read_4(rman_get_bustag((sc)->cpu_p_blk), \ 67fec754d4SMike Smith rman_get_bushandle((sc)->cpu_p_blk), \ 68fec754d4SMike Smith 0)) 69fec754d4SMike Smith #define CPU_SET_P_CNT(sc, val) (bus_space_write_4(rman_get_bustag((sc)->cpu_p_blk), \ 70fec754d4SMike Smith rman_get_bushandle((sc)->cpu_p_blk), \ 71fec754d4SMike Smith 0, (val))) 72fec754d4SMike Smith #define CPU_P_CNT_THT_EN (1<<4) 73fec754d4SMike Smith }; 74fec754d4SMike Smith 75fec754d4SMike Smith /* 76fec754d4SMike Smith * Speeds are stored in counts, from 1 - CPU_MAX_SPEED, and 77fec754d4SMike Smith * reported to the user in tenths of a percent. 78fec754d4SMike Smith */ 79fec754d4SMike Smith static u_int32_t cpu_duty_offset; 80fec754d4SMike Smith static u_int32_t cpu_duty_width; 81fec754d4SMike Smith #define CPU_MAX_SPEED (1 << cpu_duty_width) 82fec754d4SMike Smith #define CPU_SPEED_PERCENT(x) ((1000 * (x)) / CPU_MAX_SPEED) 83fec754d4SMike Smith #define CPU_SPEED_PRINTABLE(x) (CPU_SPEED_PERCENT(x) / 10),(CPU_SPEED_PERCENT(x) % 10) 84fec754d4SMike Smith 85fec754d4SMike Smith static u_int32_t cpu_smi_cmd; /* should be a generic way to do this */ 86fec754d4SMike Smith static u_int8_t cpu_pstate_cnt; 87fec754d4SMike Smith 88fec754d4SMike Smith static u_int32_t cpu_current_state; 89fec754d4SMike Smith static u_int32_t cpu_performance_state; 90fec754d4SMike Smith static u_int32_t cpu_economy_state; 91fec754d4SMike Smith static u_int32_t cpu_max_state; 92fec754d4SMike Smith 93fec754d4SMike Smith static device_t *cpu_devices; 94fec754d4SMike Smith static int cpu_ndevices; 95fec754d4SMike Smith 96fec754d4SMike Smith static struct sysctl_ctx_list acpi_cpu_sysctl_ctx; 97fec754d4SMike Smith static struct sysctl_oid *acpi_cpu_sysctl_tree; 98fec754d4SMike Smith 99fec754d4SMike Smith static int acpi_cpu_probe(device_t dev); 100fec754d4SMike Smith static int acpi_cpu_attach(device_t dev); 101fec754d4SMike Smith static void acpi_cpu_init_throttling(void *arg); 102fec754d4SMike Smith static void acpi_cpu_set_speed(u_int32_t speed); 103899ccf54SMitsuru IWASAKI static void acpi_cpu_power_profile(void *arg); 104fec754d4SMike Smith static int acpi_cpu_speed_sysctl(SYSCTL_HANDLER_ARGS); 105fec754d4SMike Smith 106fec754d4SMike Smith static device_method_t acpi_cpu_methods[] = { 107fec754d4SMike Smith /* Device interface */ 108fec754d4SMike Smith DEVMETHOD(device_probe, acpi_cpu_probe), 109fec754d4SMike Smith DEVMETHOD(device_attach, acpi_cpu_attach), 110fec754d4SMike Smith 111fec754d4SMike Smith {0, 0} 112fec754d4SMike Smith }; 113fec754d4SMike Smith 114fec754d4SMike Smith static driver_t acpi_cpu_driver = { 115fec754d4SMike Smith "acpi_cpu", 116fec754d4SMike Smith acpi_cpu_methods, 117fec754d4SMike Smith sizeof(struct acpi_cpu_softc), 118fec754d4SMike Smith }; 119fec754d4SMike Smith 1203273b005SMike Smith static devclass_t acpi_cpu_devclass; 121fec754d4SMike Smith DRIVER_MODULE(acpi_cpu, acpi, acpi_cpu_driver, acpi_cpu_devclass, 0, 0); 122fec754d4SMike Smith 123fec754d4SMike Smith static int 124fec754d4SMike Smith acpi_cpu_probe(device_t dev) 125fec754d4SMike Smith { 126f48bf2d7SMike Smith if (!acpi_disabled("cpu") && 127f48bf2d7SMike Smith (acpi_get_type(dev) == ACPI_TYPE_PROCESSOR)) { 128fec754d4SMike Smith device_set_desc(dev, "CPU"); /* XXX get more verbose description? */ 129fec754d4SMike Smith return(0); 130fec754d4SMike Smith } 131fec754d4SMike Smith return(ENXIO); 132fec754d4SMike Smith } 133fec754d4SMike Smith 134fec754d4SMike Smith static int 135fec754d4SMike Smith acpi_cpu_attach(device_t dev) 136fec754d4SMike Smith { 137fec754d4SMike Smith struct acpi_cpu_softc *sc; 138fec754d4SMike Smith struct acpi_softc *acpi_sc; 139fec754d4SMike Smith ACPI_OBJECT processor; 140fec754d4SMike Smith ACPI_BUFFER buf; 141fec754d4SMike Smith ACPI_STATUS status; 142fec754d4SMike Smith u_int32_t p_blk; 143fec754d4SMike Smith u_int32_t p_blk_length; 144fec754d4SMike Smith u_int32_t duty_end; 145fec754d4SMike Smith int rid; 146fec754d4SMike Smith 147b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 148fec754d4SMike Smith 149fec754d4SMike Smith ACPI_ASSERTLOCK; 150fec754d4SMike Smith 151fec754d4SMike Smith sc = device_get_softc(dev); 152fec754d4SMike Smith sc->cpu_dev = dev; 153fec754d4SMike Smith sc->cpu_handle = acpi_get_handle(dev); 154fec754d4SMike Smith 155fec754d4SMike Smith /* 156fec754d4SMike Smith * Get global parameters from the FADT. 157fec754d4SMike Smith */ 158fec754d4SMike Smith if (device_get_unit(sc->cpu_dev) == 0) { 159ad5dc75bSMike Smith cpu_duty_offset = AcpiGbl_FADT->DutyOffset; 160ad5dc75bSMike Smith cpu_duty_width = AcpiGbl_FADT->DutyWidth; 161ad5dc75bSMike Smith cpu_smi_cmd = AcpiGbl_FADT->SmiCmd; 162ad5dc75bSMike Smith cpu_pstate_cnt = AcpiGbl_FADT->PstateCnt; 163fec754d4SMike Smith 164fec754d4SMike Smith /* validate the offset/width */ 1653e759f36SMike Smith if (cpu_duty_width > 0) { 166fec754d4SMike Smith duty_end = cpu_duty_offset + cpu_duty_width - 1; 167fec754d4SMike Smith /* check that it fits */ 168fec754d4SMike Smith if (duty_end > 31) { 169fec754d4SMike Smith printf("acpi_cpu: CLK_VAL field overflows P_CNT register\n"); 170fec754d4SMike Smith cpu_duty_width = 0; 171fec754d4SMike Smith } 172fec754d4SMike Smith /* check for overlap with the THT_EN bit */ 173fec754d4SMike Smith if ((cpu_duty_offset <= 4) && (duty_end >= 4)) { 174fec754d4SMike Smith printf("acpi_cpu: CLK_VAL field overlaps THT_EN bit\n"); 175fec754d4SMike Smith cpu_duty_width = 0; 176fec754d4SMike Smith } 1773e759f36SMike Smith } 178fec754d4SMike Smith 179fec754d4SMike Smith /* 180fec754d4SMike Smith * Start the throttling process once the probe phase completes, if we think that 181fec754d4SMike Smith * it's going to be useful. If the duty width value is zero, there are no significant 182fec754d4SMike Smith * bits in the register and thus no throttled states. 183fec754d4SMike Smith */ 184fec754d4SMike Smith if (cpu_duty_width > 0) { 185fec754d4SMike Smith AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_cpu_init_throttling, NULL); 186fec754d4SMike Smith 187fec754d4SMike Smith acpi_sc = acpi_device_get_parent_softc(dev); 188fec754d4SMike Smith sysctl_ctx_init(&acpi_cpu_sysctl_ctx); 189fec754d4SMike Smith acpi_cpu_sysctl_tree = SYSCTL_ADD_NODE(&acpi_cpu_sysctl_ctx, 190fec754d4SMike Smith SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), 191fec754d4SMike Smith OID_AUTO, "cpu", CTLFLAG_RD, 0, ""); 192fec754d4SMike Smith 193fec754d4SMike Smith SYSCTL_ADD_INT(&acpi_cpu_sysctl_ctx, SYSCTL_CHILDREN(acpi_cpu_sysctl_tree), 194fec754d4SMike Smith OID_AUTO, "max_speed", CTLFLAG_RD, 195fec754d4SMike Smith &cpu_max_state, 0, "maximum CPU speed"); 196fec754d4SMike Smith SYSCTL_ADD_INT(&acpi_cpu_sysctl_ctx, SYSCTL_CHILDREN(acpi_cpu_sysctl_tree), 197fec754d4SMike Smith OID_AUTO, "current_speed", CTLFLAG_RD, 198fec754d4SMike Smith &cpu_current_state, 0, "current CPU speed"); 199fec754d4SMike Smith SYSCTL_ADD_PROC(&acpi_cpu_sysctl_ctx, SYSCTL_CHILDREN(acpi_cpu_sysctl_tree), 200fec754d4SMike Smith OID_AUTO, "performance_speed", CTLTYPE_INT | CTLFLAG_RW, 201fec754d4SMike Smith &cpu_performance_state, 0, acpi_cpu_speed_sysctl, "I", ""); 202fec754d4SMike Smith SYSCTL_ADD_PROC(&acpi_cpu_sysctl_ctx, SYSCTL_CHILDREN(acpi_cpu_sysctl_tree), 203fec754d4SMike Smith OID_AUTO, "economy_speed", CTLTYPE_INT | CTLFLAG_RW, 204fec754d4SMike Smith &cpu_economy_state, 0, acpi_cpu_speed_sysctl, "I", ""); 205fec754d4SMike Smith } 206fec754d4SMike Smith } 207fec754d4SMike Smith 208fec754d4SMike Smith /* 209fec754d4SMike Smith * Get the processor object. 210fec754d4SMike Smith */ 211fec754d4SMike Smith buf.Pointer = &processor; 212fec754d4SMike Smith buf.Length = sizeof(processor); 213fec754d4SMike Smith if (ACPI_FAILURE(status = AcpiEvaluateObject(sc->cpu_handle, NULL, NULL, &buf))) { 214bfae45aaSMike Smith device_printf(sc->cpu_dev, "couldn't get Processor object - %s\n", AcpiFormatException(status)); 215fec754d4SMike Smith return_VALUE(ENXIO); 216fec754d4SMike Smith } 217fec754d4SMike Smith if (processor.Type != ACPI_TYPE_PROCESSOR) { 218fec754d4SMike Smith device_printf(sc->cpu_dev, "Processor object has bad type %d\n", processor.Type); 219fec754d4SMike Smith return_VALUE(ENXIO); 220fec754d4SMike Smith } 221fec754d4SMike Smith sc->cpu_id = processor.Processor.ProcId; 222fec754d4SMike Smith 223fec754d4SMike Smith /* 224fec754d4SMike Smith * If it looks like we support throttling, find this CPU's P_BLK. 225fec754d4SMike Smith * 226fec754d4SMike Smith * Note that some systems seem to duplicate the P_BLK pointer across 227fec754d4SMike Smith * multiple CPUs, so not getting the resource is not fatal. 228fec754d4SMike Smith * 229fec754d4SMike Smith * XXX should support _PTC here as well, once we work out how to parse it. 230fec754d4SMike Smith * 231fec754d4SMike Smith * XXX is it valid to assume that the P_BLK must be 6 bytes long? 232fec754d4SMike Smith */ 233fec754d4SMike Smith if (cpu_duty_width > 0) { 234fec754d4SMike Smith p_blk = processor.Processor.PblkAddress; 235fec754d4SMike Smith p_blk_length = processor.Processor.PblkLength; 236fec754d4SMike Smith 237fec754d4SMike Smith /* allocate bus space if possible */ 238fec754d4SMike Smith if ((p_blk > 0) && (p_blk_length == 6)) { 239fec754d4SMike Smith rid = 0; 240fec754d4SMike Smith bus_set_resource(sc->cpu_dev, SYS_RES_IOPORT, rid, p_blk, p_blk_length); 241fec754d4SMike Smith sc->cpu_p_blk = bus_alloc_resource(sc->cpu_dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, 242fec754d4SMike Smith RF_ACTIVE); 243fec754d4SMike Smith 2444c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_IO, "acpi_cpu%d: throttling with P_BLK at 0x%x/%d%s\n", 245fec754d4SMike Smith device_get_unit(sc->cpu_dev), p_blk, p_blk_length, 246f0987736SMitsuru IWASAKI sc->cpu_p_blk ? "" : " (shadowed)")); 247fec754d4SMike Smith } 248fec754d4SMike Smith } 249fec754d4SMike Smith return_VALUE(0); 250fec754d4SMike Smith } 251fec754d4SMike Smith 252fec754d4SMike Smith /* 253fec754d4SMike Smith * Call this *after* all CPUs have been attached. 254fec754d4SMike Smith * 255fec754d4SMike Smith * Takes the ACPI lock to avoid fighting anyone over the SMI command 256fec754d4SMike Smith * port. Could probably lock less code. 257fec754d4SMike Smith */ 258fec754d4SMike Smith static void 259fec754d4SMike Smith acpi_cpu_init_throttling(void *arg) 260fec754d4SMike Smith { 261e5e5b51fSJohn Baldwin int cpu_temp_speed; 262fec754d4SMike Smith 263fec754d4SMike Smith ACPI_LOCK; 264fec754d4SMike Smith 265fec754d4SMike Smith /* get set of CPU devices */ 266fec754d4SMike Smith devclass_get_devices(acpi_cpu_devclass, &cpu_devices, &cpu_ndevices); 267fec754d4SMike Smith 268fec754d4SMike Smith /* initialise throttling states */ 269fec754d4SMike Smith cpu_max_state = CPU_MAX_SPEED; 270fec754d4SMike Smith cpu_performance_state = cpu_max_state; 271fec754d4SMike Smith cpu_economy_state = cpu_performance_state / 2; 272fec754d4SMike Smith if (cpu_economy_state == 0) /* 0 is 'reserved' */ 273fec754d4SMike Smith cpu_economy_state++; 274e5e5b51fSJohn Baldwin if (TUNABLE_INT_FETCH("hw.acpi.cpu.performance_speed", 275e5e5b51fSJohn Baldwin &cpu_temp_speed) && cpu_temp_speed > 0 && 276e5e5b51fSJohn Baldwin cpu_temp_speed <= cpu_max_state) 277e5e5b51fSJohn Baldwin cpu_performance_state = cpu_temp_speed; 278e5e5b51fSJohn Baldwin if (TUNABLE_INT_FETCH("hw.acpi.cpu.economy_speed", 279e5e5b51fSJohn Baldwin &cpu_temp_speed) && cpu_temp_speed > 0 && 280e5e5b51fSJohn Baldwin cpu_temp_speed <= cpu_max_state) 281e5e5b51fSJohn Baldwin cpu_economy_state = cpu_temp_speed; 282fec754d4SMike Smith 283fec754d4SMike Smith /* register performance profile change handler */ 284899ccf54SMitsuru IWASAKI EVENTHANDLER_REGISTER(power_profile_change, acpi_cpu_power_profile, NULL, 0); 285fec754d4SMike Smith 286fec754d4SMike Smith /* if ACPI 2.0+, signal platform that we are taking over throttling */ 287fec754d4SMike Smith if (cpu_pstate_cnt != 0) { 288fec754d4SMike Smith /* XXX should be a generic interface for this */ 289ad5dc75bSMike Smith AcpiOsWritePort(cpu_smi_cmd, cpu_pstate_cnt, 8); 290fec754d4SMike Smith } 291fec754d4SMike Smith 292fec754d4SMike Smith ACPI_UNLOCK; 293fec754d4SMike Smith 294fec754d4SMike Smith /* set initial speed */ 295899ccf54SMitsuru IWASAKI acpi_cpu_power_profile(NULL); 296fec754d4SMike Smith 297fec754d4SMike Smith printf("acpi_cpu: CPU throttling enabled, %d steps from 100%% to %d.%d%%\n", 298fec754d4SMike Smith CPU_MAX_SPEED, CPU_SPEED_PRINTABLE(1)); 299fec754d4SMike Smith } 300fec754d4SMike Smith 301fec754d4SMike Smith /* 302fec754d4SMike Smith * Set CPUs to the new state. 303fec754d4SMike Smith * 304fec754d4SMike Smith * Must be called with the ACPI lock held. 305fec754d4SMike Smith */ 306fec754d4SMike Smith static void 307fec754d4SMike Smith acpi_cpu_set_speed(u_int32_t speed) 308fec754d4SMike Smith { 309fec754d4SMike Smith struct acpi_cpu_softc *sc; 310fec754d4SMike Smith int i; 311fec754d4SMike Smith u_int32_t p_cnt, clk_val; 312fec754d4SMike Smith 313fec754d4SMike Smith ACPI_ASSERTLOCK; 314fec754d4SMike Smith 315fec754d4SMike Smith /* iterate over processors */ 316fec754d4SMike Smith for (i = 0; i < cpu_ndevices; i++) { 317fec754d4SMike Smith sc = device_get_softc(cpu_devices[i]); 318fec754d4SMike Smith if (sc->cpu_p_blk == NULL) 319fec754d4SMike Smith continue; 320fec754d4SMike Smith 321fec754d4SMike Smith /* get the current P_CNT value and disable throttling */ 322fec754d4SMike Smith p_cnt = CPU_GET_P_CNT(sc); 323fec754d4SMike Smith p_cnt &= ~CPU_P_CNT_THT_EN; 324fec754d4SMike Smith CPU_SET_P_CNT(sc, p_cnt); 325fec754d4SMike Smith 326fec754d4SMike Smith /* if we're at maximum speed, that's all */ 327fec754d4SMike Smith if (speed < CPU_MAX_SPEED) { 328fec754d4SMike Smith 329fec754d4SMike Smith /* mask the old CLK_VAL off and or-in the new value */ 330fec754d4SMike Smith clk_val = CPU_MAX_SPEED << cpu_duty_offset; 331fec754d4SMike Smith p_cnt &= ~clk_val; 332fec754d4SMike Smith p_cnt |= (speed << cpu_duty_offset); 333fec754d4SMike Smith 334fec754d4SMike Smith /* write the new P_CNT value and then enable throttling */ 335fec754d4SMike Smith CPU_SET_P_CNT(sc, p_cnt); 336fec754d4SMike Smith p_cnt |= CPU_P_CNT_THT_EN; 337fec754d4SMike Smith CPU_SET_P_CNT(sc, p_cnt); 338fec754d4SMike Smith } 3396971b3c7SMitsuru IWASAKI ACPI_VPRINT(sc->cpu_dev, acpi_device_get_parent_softc(sc->cpu_dev), 3406971b3c7SMitsuru IWASAKI "set speed to %d.%d%%\n", CPU_SPEED_PRINTABLE(speed)); 341fec754d4SMike Smith } 342fec754d4SMike Smith cpu_current_state = speed; 343fec754d4SMike Smith } 344fec754d4SMike Smith 345fec754d4SMike Smith /* 346fec754d4SMike Smith * Power profile change hook. 347fec754d4SMike Smith * 348fec754d4SMike Smith * Uses the ACPI lock to avoid reentrancy. 349fec754d4SMike Smith */ 350fec754d4SMike Smith static void 351899ccf54SMitsuru IWASAKI acpi_cpu_power_profile(void *arg) 352fec754d4SMike Smith { 353899ccf54SMitsuru IWASAKI int state; 354fec754d4SMike Smith u_int32_t new; 355fec754d4SMike Smith 356899ccf54SMitsuru IWASAKI state = power_profile_get_state(); 357899ccf54SMitsuru IWASAKI if (state != POWER_PROFILE_PERFORMANCE && 358899ccf54SMitsuru IWASAKI state != POWER_PROFILE_ECONOMY) { 359899ccf54SMitsuru IWASAKI return; 360899ccf54SMitsuru IWASAKI } 361899ccf54SMitsuru IWASAKI 362fec754d4SMike Smith ACPI_LOCK; 363fec754d4SMike Smith 364899ccf54SMitsuru IWASAKI switch (state) { 365899ccf54SMitsuru IWASAKI case POWER_PROFILE_PERFORMANCE: 366899ccf54SMitsuru IWASAKI new = cpu_performance_state; 367899ccf54SMitsuru IWASAKI break; 368899ccf54SMitsuru IWASAKI case POWER_PROFILE_ECONOMY: 369899ccf54SMitsuru IWASAKI new = cpu_economy_state; 370899ccf54SMitsuru IWASAKI break; 371899ccf54SMitsuru IWASAKI default: 372899ccf54SMitsuru IWASAKI new = cpu_current_state; 373899ccf54SMitsuru IWASAKI break; 374899ccf54SMitsuru IWASAKI } 375899ccf54SMitsuru IWASAKI 376fec754d4SMike Smith if (cpu_current_state != new) 377fec754d4SMike Smith acpi_cpu_set_speed(new); 378fec754d4SMike Smith 379fec754d4SMike Smith ACPI_UNLOCK; 380fec754d4SMike Smith } 381fec754d4SMike Smith 382fec754d4SMike Smith /* 383fec754d4SMike Smith * Handle changes in the performance/ecomony CPU settings. 384fec754d4SMike Smith * 385fec754d4SMike Smith * Does not need the ACPI lock (although setting *argp should 386fec754d4SMike Smith * probably be atomic). 387fec754d4SMike Smith */ 388fec754d4SMike Smith static int 389fec754d4SMike Smith acpi_cpu_speed_sysctl(SYSCTL_HANDLER_ARGS) 390fec754d4SMike Smith { 391fec754d4SMike Smith u_int32_t *argp; 392fec754d4SMike Smith u_int32_t arg; 393fec754d4SMike Smith int error; 394fec754d4SMike Smith 395fec754d4SMike Smith argp = (u_int32_t *)oidp->oid_arg1; 396fec754d4SMike Smith arg = *argp; 397fec754d4SMike Smith error = sysctl_handle_int(oidp, &arg, 0, req); 398fec754d4SMike Smith 399fec754d4SMike Smith /* error or no new value */ 400fec754d4SMike Smith if ((error != 0) || (req->newptr == NULL)) 401fec754d4SMike Smith return(error); 402fec754d4SMike Smith 403fec754d4SMike Smith /* range check */ 404f0987736SMitsuru IWASAKI if ((arg < 1) || (arg > cpu_max_state)) 405fec754d4SMike Smith return(EINVAL); 406fec754d4SMike Smith 407fec754d4SMike Smith /* set new value and possibly switch */ 408fec754d4SMike Smith *argp = arg; 409899ccf54SMitsuru IWASAKI acpi_cpu_power_profile(NULL); 410fec754d4SMike Smith 411fec754d4SMike Smith return(0); 412fec754d4SMike Smith } 413