acpi_perf.c revision 141411
1228763Smm/*- 2228753Smm * Copyright (c) 2003-2005 Nate Lawson (SDG) 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/dev/acpica/acpi_perf.c 141411 2005-02-06 20:12:28Z njl $"); 29 30#include "opt_acpi.h" 31#include <sys/param.h> 32#include <sys/kernel.h> 33#include <sys/proc.h> 34#include <sys/sched.h> 35#include <sys/bus.h> 36#include <sys/cpu.h> 37#include <sys/power.h> 38#include <sys/malloc.h> 39#include <sys/module.h> 40#include <sys/sbuf.h> 41#include <sys/pcpu.h> 42 43#include <machine/bus_pio.h> 44#include <machine/bus.h> 45#include <machine/resource.h> 46#include <sys/rman.h> 47 48#include "acpi.h" 49#include <dev/acpica/acpivar.h> 50 51#include "cpufreq_if.h" 52 53/* 54 * Support for ACPI processor performance states (Px) according to 55 * section x of the ACPI specification. 56 */ 57 58struct acpi_px { 59 uint32_t core_freq; 60 uint32_t power; 61 uint32_t trans_lat; 62 uint32_t bm_lat; 63 uint32_t ctrl_val; 64 uint32_t sts_val; 65}; 66 67#define MAX_PX_STATES 16 68 69struct acpi_perf_softc { 70 device_t dev; 71 ACPI_HANDLE handle; 72 struct resource *perf_ctrl; /* Set new performance state. */ 73 int perf_ctrl_type; /* Resource type for perf_ctrl. */ 74 struct resource *perf_status; /* Check that transition succeeded. */ 75 int perf_sts_type; /* Resource type for perf_status. */ 76 struct acpi_px *px_states; /* ACPI perf states. */ 77 uint32_t px_count; /* Total number of perf states. */ 78 uint32_t px_max_avail; /* Lowest index state available. */ 79 int px_curr_state; /* Active state index. */ 80 int px_rid; 81}; 82 83#define PX_GET_REG(reg) \ 84 (bus_space_read_4(rman_get_bustag((reg)), \ 85 rman_get_bushandle((reg)), 0)) 86#define PX_SET_REG(reg, val) \ 87 (bus_space_write_4(rman_get_bustag((reg)), \ 88 rman_get_bushandle((reg)), 0, (val))) 89 90static void acpi_perf_identify(driver_t *driver, device_t parent); 91static int acpi_perf_probe(device_t dev); 92static int acpi_perf_attach(device_t dev); 93static int acpi_perf_detach(device_t dev); 94static int acpi_perf_evaluate(device_t dev); 95static int acpi_px_to_set(device_t dev, struct acpi_px *px, 96 struct cf_setting *set); 97static void acpi_px_available(struct acpi_perf_softc *sc); 98static void acpi_px_startup(void *arg); 99static void acpi_px_notify(ACPI_HANDLE h, UINT32 notify, void *context); 100static int acpi_px_settings(device_t dev, struct cf_setting *sets, 101 int *count, int *type); 102static int acpi_px_set(device_t dev, const struct cf_setting *set); 103static int acpi_px_get(device_t dev, struct cf_setting *set); 104 105static device_method_t acpi_perf_methods[] = { 106 /* Device interface */ 107 DEVMETHOD(device_identify, acpi_perf_identify), 108 DEVMETHOD(device_probe, acpi_perf_probe), 109 DEVMETHOD(device_attach, acpi_perf_attach), 110 DEVMETHOD(device_detach, acpi_perf_detach), 111 112 /* cpufreq interface */ 113 DEVMETHOD(cpufreq_drv_set, acpi_px_set), 114 DEVMETHOD(cpufreq_drv_get, acpi_px_get), 115 DEVMETHOD(cpufreq_drv_settings, acpi_px_settings), 116 {0, 0} 117}; 118 119static driver_t acpi_perf_driver = { 120 "acpi_perf", 121 acpi_perf_methods, 122 sizeof(struct acpi_perf_softc), 123}; 124 125static devclass_t acpi_perf_devclass; 126DRIVER_MODULE(acpi_perf, cpu, acpi_perf_driver, acpi_perf_devclass, 0, 0); 127MODULE_DEPEND(acpi_perf, acpi, 1, 1, 1); 128 129MALLOC_DEFINE(M_ACPIPERF, "acpi_perf", "ACPI Performance states"); 130 131static void 132acpi_perf_identify(driver_t *driver, device_t parent) 133{ 134 ACPI_HANDLE handle; 135 136 /* Make sure we're not being doubly invoked. */ 137 if (device_find_child(parent, "acpi_perf", 0) != NULL) 138 return; 139 140 /* Get the handle for the Processor object and check for perf states. */ 141 handle = acpi_get_handle(parent); 142 if (handle == NULL) 143 return; 144 if (ACPI_FAILURE(AcpiEvaluateObject(handle, "_PSS", NULL, NULL))) 145 return; 146 if (BUS_ADD_CHILD(parent, 0, "acpi_perf", 0) == NULL) 147 device_printf(parent, "acpi_perf: add child failed\n"); 148} 149 150static int 151acpi_perf_probe(device_t dev) 152{ 153 ACPI_HANDLE handle; 154 ACPI_OBJECT *pkg; 155 struct resource *res; 156 ACPI_BUFFER buf; 157 int error, rid, type; 158 159 /* 160 * Check the performance state registers. If they are of type 161 * functional fixed hardware, we don't attach to allow a more 162 * specific hardware driver to manage this CPU. 163 */ 164 error = ENXIO; 165 handle = acpi_get_handle(dev); 166 buf.Pointer = NULL; 167 buf.Length = ACPI_ALLOCATE_BUFFER; 168 if (ACPI_FAILURE(AcpiEvaluateObject(handle, "_PCT", NULL, &buf))) 169 return (error); 170 pkg = (ACPI_OBJECT *)buf.Pointer; 171 172 rid = 0; 173 if (ACPI_PKG_VALID(pkg, 2) && 174 acpi_PkgGas(dev, pkg, 0, &type, &rid, &res) == 0) { 175 bus_release_resource(dev, type, rid, res); 176 device_set_desc(dev, "ACPI CPU Frequency Control"); 177 error = -10; 178 } 179 AcpiOsFree(buf.Pointer); 180 181 return (error); 182} 183 184static int 185acpi_perf_attach(device_t dev) 186{ 187 struct acpi_perf_softc *sc; 188 189 sc = device_get_softc(dev); 190 sc->dev = dev; 191 sc->handle = acpi_get_handle(dev); 192 sc->px_max_avail = 0; 193 sc->px_curr_state = CPUFREQ_VAL_UNKNOWN; 194 if (acpi_perf_evaluate(dev) != 0) 195 return (ENXIO); 196 cpufreq_register(dev); 197 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_px_startup, NULL); 198 199 return (0); 200} 201 202static int 203acpi_perf_detach(device_t dev) 204{ 205 /* TODO: teardown registers, remove notify handler. */ 206 return (ENXIO); 207} 208 209/* Probe and setup any valid performance states (Px). */ 210static int 211acpi_perf_evaluate(device_t dev) 212{ 213 struct acpi_perf_softc *sc; 214 ACPI_BUFFER buf; 215 ACPI_OBJECT *pkg, *res; 216 ACPI_STATUS status; 217 int error, i, j; 218 uint32_t *p; 219 220 /* Get the control values and parameters for each state. */ 221 error = ENXIO; 222 sc = device_get_softc(dev); 223 buf.Pointer = NULL; 224 buf.Length = ACPI_ALLOCATE_BUFFER; 225 status = AcpiEvaluateObject(sc->handle, "_PSS", NULL, &buf); 226 if (ACPI_FAILURE(status)) 227 return (ENXIO); 228 229 pkg = (ACPI_OBJECT *)buf.Pointer; 230 if (!ACPI_PKG_VALID(pkg, 1)) { 231 device_printf(dev, "invalid top level _PSS package\n"); 232 goto out; 233 } 234 sc->px_count = pkg->Package.Count; 235 236 sc->px_states = malloc(sc->px_count * sizeof(struct acpi_px), 237 M_ACPIPERF, M_WAITOK | M_ZERO); 238 if (sc->px_states == NULL) 239 goto out; 240 241 /* 242 * Each state is a package of {CoreFreq, Power, TransitionLatency, 243 * BusMasterLatency, ControlVal, StatusVal}, sorted from highest 244 * performance to lowest. 245 */ 246 for (i = 0; i < sc->px_count; i++) { 247 res = &pkg->Package.Elements[i]; 248 if (!ACPI_PKG_VALID(res, 6)) { 249 device_printf(dev, "invalid _PSS package\n"); 250 continue; 251 } 252 p = &sc->px_states[i].core_freq; 253 for (j = 0; j < 6; j++, p++) 254 acpi_PkgInt32(res, j, p); 255 } 256 AcpiOsFree(buf.Pointer); 257 258 /* Get the control and status registers (one of each). */ 259 buf.Pointer = NULL; 260 buf.Length = ACPI_ALLOCATE_BUFFER; 261 status = AcpiEvaluateObject(sc->handle, "_PCT", NULL, &buf); 262 if (ACPI_FAILURE(status)) 263 goto out; 264 265 /* Check the package of two registers, each a Buffer in GAS format. */ 266 pkg = (ACPI_OBJECT *)buf.Pointer; 267 if (!ACPI_PKG_VALID(pkg, 2)) { 268 device_printf(dev, "invalid perf register package\n"); 269 goto out; 270 } 271 272 error = acpi_PkgGas(sc->dev, pkg, 0, &sc->perf_ctrl_type, &sc->px_rid, 273 &sc->perf_ctrl); 274 if (error) { 275 if (error != EOPNOTSUPP) 276 device_printf(dev, "failed in PERF_CTL attach\n"); 277 goto out; 278 } 279 sc->px_rid++; 280 281 error = acpi_PkgGas(sc->dev, pkg, 1, &sc->perf_sts_type, &sc->px_rid, 282 &sc->perf_status); 283 if (error) { 284 if (error != EOPNOTSUPP) 285 device_printf(dev, "failed in PERF_STATUS attach\n"); 286 goto out; 287 } 288 sc->px_rid++; 289 290 /* Get our current limit and register for notifies. */ 291 acpi_px_available(sc); 292 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY, 293 acpi_px_notify, sc); 294 error = 0; 295 296out: 297 if (error) { 298 if (sc->px_states) 299 free(sc->px_states, M_ACPIPERF); 300 sc->px_count = 0; 301 } 302 if (buf.Pointer) 303 AcpiOsFree(buf.Pointer); 304 return (error); 305} 306 307static void 308acpi_px_startup(void *arg) 309{ 310 311 /* Signal to the platform that we are taking over CPU control. */ 312 if (AcpiGbl_FADT->PstateCnt == 0) 313 return; 314 ACPI_LOCK(acpi); 315 AcpiOsWritePort(AcpiGbl_FADT->SmiCmd, AcpiGbl_FADT->PstateCnt, 8); 316 ACPI_UNLOCK(acpi); 317} 318 319static void 320acpi_px_notify(ACPI_HANDLE h, UINT32 notify, void *context) 321{ 322 struct acpi_perf_softc *sc; 323 324 sc = context; 325 acpi_px_available(sc); 326 327 /* TODO: Implement notification when frequency changes. */ 328} 329 330/* 331 * Find the highest currently-supported performance state. 332 * This can be called at runtime (e.g., due to a docking event) at 333 * the request of a Notify on the processor object. 334 */ 335static void 336acpi_px_available(struct acpi_perf_softc *sc) 337{ 338 ACPI_STATUS status; 339 struct cf_setting set; 340 341 status = acpi_GetInteger(sc->handle, "_PPC", &sc->px_max_avail); 342 343 /* If the old state is too high, set current state to the new max. */ 344 if (ACPI_SUCCESS(status)) { 345 if (sc->px_curr_state != CPUFREQ_VAL_UNKNOWN && 346 sc->px_curr_state > sc->px_max_avail) { 347 acpi_px_to_set(sc->dev, 348 &sc->px_states[sc->px_max_avail], &set); 349 acpi_px_set(sc->dev, &set); 350 } 351 } else 352 sc->px_max_avail = 0; 353} 354 355static int 356acpi_px_to_set(device_t dev, struct acpi_px *px, struct cf_setting *set) 357{ 358 359 if (px == NULL || set == NULL) 360 return (EINVAL); 361 362 set->freq = px->core_freq; 363 set->power = px->power; 364 /* XXX Include BM latency too? */ 365 set->lat = px->trans_lat; 366 set->volts = CPUFREQ_VAL_UNKNOWN; 367 set->dev = dev; 368 369 return (0); 370} 371 372static int 373acpi_px_settings(device_t dev, struct cf_setting *sets, int *count, int *type) 374{ 375 struct acpi_perf_softc *sc; 376 int x, y; 377 378 sc = device_get_softc(dev); 379 if (sets == NULL || count == NULL) 380 return (EINVAL); 381 if (*count < sc->px_count - sc->px_max_avail) 382 return (ENOMEM); 383 384 /* Return a list of settings that are currently valid. */ 385 y = 0; 386 for (x = sc->px_max_avail; x < sc->px_count; x++, y++) 387 acpi_px_to_set(dev, &sc->px_states[x], &sets[y]); 388 *count = sc->px_count - sc->px_max_avail; 389 *type = CPUFREQ_TYPE_ABSOLUTE; 390 391 return (0); 392} 393 394static int 395acpi_px_set(device_t dev, const struct cf_setting *set) 396{ 397 struct acpi_perf_softc *sc; 398 int i, status, sts_val, tries; 399 400 if (set == NULL) 401 return (EINVAL); 402 sc = device_get_softc(dev); 403 404 /* Look up appropriate state, based on frequency. */ 405 for (i = sc->px_max_avail; i < sc->px_count; i++) { 406 if (CPUFREQ_CMP(set->freq, sc->px_states[i].core_freq)) 407 break; 408 } 409 if (i == sc->px_count) 410 return (EINVAL); 411 412 /* Write the appropriate value to the register. */ 413 PX_SET_REG(sc->perf_ctrl, sc->px_states[i].ctrl_val); 414 415 /* Try for up to 1 ms to verify the desired state was selected. */ 416 sts_val = sc->px_states[i].sts_val; 417 for (tries = 0; tries < 100; tries++) { 418 status = PX_GET_REG(sc->perf_status); 419 if (status == sts_val) 420 break; 421 DELAY(10); 422 } 423 if (tries == 100) { 424 device_printf(dev, "Px transition to %d failed\n", 425 sc->px_states[i].core_freq); 426 return (ENXIO); 427 } 428 sc->px_curr_state = i; 429 430 return (0); 431} 432 433static int 434acpi_px_get(device_t dev, struct cf_setting *set) 435{ 436 struct acpi_perf_softc *sc; 437 uint64_t rate; 438 int i; 439 struct pcpu *pc; 440 441 if (set == NULL) 442 return (EINVAL); 443 sc = device_get_softc(dev); 444 445 /* If we've set the rate before, use the cached value. */ 446 if (sc->px_curr_state != CPUFREQ_VAL_UNKNOWN) { 447 acpi_px_to_set(dev, &sc->px_states[sc->px_curr_state], set); 448 return (0); 449 } 450 451 /* Otherwise, estimate and try to match against our settings. */ 452 pc = cpu_get_pcpu(dev); 453 if (pc == NULL) 454 return (ENXIO); 455 cpu_est_clockrate(pc->pc_cpuid, &rate); 456 rate /= 1000000; 457 for (i = 0; i < sc->px_count; i++) { 458 if (CPUFREQ_CMP(sc->px_states[i].core_freq, rate)) { 459 sc->px_curr_state = i; 460 acpi_px_to_set(dev, &sc->px_states[i], set); 461 break; 462 } 463 } 464 465 /* No match, give up. */ 466 if (i == sc->px_count) { 467 sc->px_curr_state = CPUFREQ_VAL_UNKNOWN; 468 set->freq = CPUFREQ_VAL_UNKNOWN; 469 } 470 471 return (0); 472} 473