1 /* $NetBSD: acpi_cpu.c,v 1.22 2010/08/27 03:05:26 jruoho Exp $ */ 2 3 /*- 4 * Copyright (c) 2010 Jukka Ruohonen <jruohonen@iki.fi> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: acpi_cpu.c,v 1.22 2010/08/27 03:05:26 jruoho Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/cpu.h> 34 #include <sys/kernel.h> 35 #include <sys/kmem.h> 36 #include <sys/module.h> 37 #include <sys/mutex.h> 38 #include <sys/once.h> 39 40 #include <dev/acpi/acpireg.h> 41 #include <dev/acpi/acpivar.h> 42 #include <dev/acpi/acpi_cpu.h> 43 44 #include <machine/acpi_machdep.h> 45 46 #define _COMPONENT ACPI_BUS_COMPONENT 47 ACPI_MODULE_NAME ("acpi_cpu") 48 49 static int acpicpu_match(device_t, cfdata_t, void *); 50 static void acpicpu_attach(device_t, device_t, void *); 51 static int acpicpu_detach(device_t, int); 52 static int acpicpu_once_attach(void); 53 static int acpicpu_once_detach(void); 54 static void acpicpu_prestart(device_t); 55 static void acpicpu_start(device_t); 56 57 static int acpicpu_object(ACPI_HANDLE, struct acpicpu_object *); 58 static cpuid_t acpicpu_id(uint32_t); 59 static uint32_t acpicpu_cap(struct acpicpu_softc *); 60 static ACPI_STATUS acpicpu_cap_pdc(struct acpicpu_softc *, uint32_t); 61 static ACPI_STATUS acpicpu_cap_osc(struct acpicpu_softc *, 62 uint32_t, uint32_t *); 63 static void acpicpu_notify(ACPI_HANDLE, uint32_t, void *); 64 static bool acpicpu_suspend(device_t, const pmf_qual_t *); 65 static bool acpicpu_resume(device_t, const pmf_qual_t *); 66 67 struct acpicpu_softc **acpicpu_sc = NULL; 68 69 static const char * const acpicpu_hid[] = { 70 "ACPI0007", 71 NULL 72 }; 73 74 CFATTACH_DECL_NEW(acpicpu, sizeof(struct acpicpu_softc), 75 acpicpu_match, acpicpu_attach, acpicpu_detach, NULL); 76 77 static int 78 acpicpu_match(device_t parent, cfdata_t match, void *aux) 79 { 80 struct acpi_attach_args *aa = aux; 81 struct acpicpu_object ao; 82 int rv; 83 84 if (aa->aa_node->ad_type != ACPI_TYPE_PROCESSOR) 85 return 0; 86 87 if (acpi_match_hid(aa->aa_node->ad_devinfo, acpicpu_hid) != 0) 88 return 1; 89 90 rv = acpicpu_object(aa->aa_node->ad_handle, &ao); 91 92 if (rv != 0 || acpicpu_id(ao.ao_procid) == 0xFFFFFF) 93 return 0; 94 95 return 1; 96 } 97 98 static void 99 acpicpu_attach(device_t parent, device_t self, void *aux) 100 { 101 struct acpicpu_softc *sc = device_private(self); 102 struct acpi_attach_args *aa = aux; 103 static ONCE_DECL(once_attach); 104 int rv; 105 106 rv = acpicpu_object(aa->aa_node->ad_handle, &sc->sc_object); 107 108 if (rv != 0) 109 return; 110 111 rv = RUN_ONCE(&once_attach, acpicpu_once_attach); 112 113 if (rv != 0) 114 return; 115 116 sc->sc_dev = self; 117 sc->sc_cold = true; 118 sc->sc_passive = false; 119 sc->sc_node = aa->aa_node; 120 sc->sc_cpuid = acpicpu_id(sc->sc_object.ao_procid); 121 122 if (sc->sc_cpuid == 0xFFFFFF) { 123 aprint_error(": invalid CPU ID\n"); 124 return; 125 } 126 127 if (acpicpu_sc[sc->sc_cpuid] != NULL) { 128 aprint_error(": already attached\n"); 129 return; 130 } 131 132 aprint_naive("\n"); 133 aprint_normal(": ACPI CPU\n"); 134 135 acpicpu_sc[sc->sc_cpuid] = sc; 136 137 sc->sc_cap = acpicpu_cap(sc); 138 sc->sc_flags |= acpicpu_md_quirks(); 139 140 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE); 141 142 acpicpu_cstate_attach(self); 143 acpicpu_pstate_attach(self); 144 acpicpu_tstate_attach(self); 145 146 (void)config_defer(self, acpicpu_prestart); 147 (void)acpi_register_notify(sc->sc_node, acpicpu_notify); 148 (void)pmf_device_register(self, acpicpu_suspend, acpicpu_resume); 149 } 150 151 static int 152 acpicpu_detach(device_t self, int flags) 153 { 154 struct acpicpu_softc *sc = device_private(self); 155 static ONCE_DECL(once_detach); 156 int rv = 0; 157 158 sc->sc_cold = true; 159 acpi_deregister_notify(sc->sc_node); 160 161 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0) 162 rv = acpicpu_cstate_detach(self); 163 164 if (rv != 0) 165 return rv; 166 167 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0) 168 rv = acpicpu_pstate_detach(self); 169 170 if (rv != 0) 171 return rv; 172 173 if ((sc->sc_flags & ACPICPU_FLAG_T) != 0) 174 rv = acpicpu_tstate_detach(self); 175 176 if (rv != 0) 177 return rv; 178 179 rv = RUN_ONCE(&once_detach, acpicpu_once_detach); 180 181 if (rv != 0) 182 return rv; 183 184 mutex_destroy(&sc->sc_mtx); 185 186 return 0; 187 } 188 189 static int 190 acpicpu_once_attach(void) 191 { 192 struct acpicpu_softc *sc; 193 unsigned int i; 194 195 acpicpu_sc = kmem_zalloc(maxcpus * sizeof(*sc), KM_SLEEP); 196 197 if (acpicpu_sc == NULL) 198 return ENOMEM; 199 200 for (i = 0; i < maxcpus; i++) 201 acpicpu_sc[i] = NULL; 202 203 return 0; 204 } 205 206 static int 207 acpicpu_once_detach(void) 208 { 209 struct acpicpu_softc *sc; 210 211 if (acpicpu_sc != NULL) 212 kmem_free(acpicpu_sc, maxcpus * sizeof(*sc)); 213 214 return 0; 215 } 216 217 static void 218 acpicpu_prestart(device_t self) 219 { 220 struct acpicpu_softc *sc = device_private(self); 221 static bool once = false; 222 223 if (once != false) { 224 sc->sc_cold = false; 225 return; 226 } 227 228 once = true; 229 230 (void)config_interrupts(self, acpicpu_start); 231 } 232 233 static void 234 acpicpu_start(device_t self) 235 { 236 struct acpicpu_softc *sc = device_private(self); 237 238 /* 239 * Run the state-specific initialization 240 * routines. These should be called only 241 * once, after interrupts are enabled and 242 * all ACPI CPUs have attached. 243 */ 244 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0) 245 acpicpu_cstate_start(self); 246 247 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0) 248 acpicpu_pstate_start(self); 249 250 if ((sc->sc_flags & ACPICPU_FLAG_T) != 0) 251 acpicpu_tstate_start(self); 252 253 aprint_debug_dev(sc->sc_dev, "ACPI CPUs started (cap " 254 "0x%02x, flags 0x%06x)\n", sc->sc_cap, sc->sc_flags); 255 256 sc->sc_cold = false; 257 } 258 259 static int 260 acpicpu_object(ACPI_HANDLE hdl, struct acpicpu_object *ao) 261 { 262 ACPI_OBJECT *obj; 263 ACPI_BUFFER buf; 264 ACPI_STATUS rv; 265 266 rv = acpi_eval_struct(hdl, NULL, &buf); 267 268 if (ACPI_FAILURE(rv)) 269 return 1; 270 271 obj = buf.Pointer; 272 273 if (obj->Type != ACPI_TYPE_PROCESSOR) { 274 rv = AE_TYPE; 275 goto out; 276 } 277 278 if (obj->Processor.ProcId > (uint32_t)maxcpus) { 279 rv = AE_LIMIT; 280 goto out; 281 } 282 283 KDASSERT((uint64_t)obj->Processor.PblkAddress < UINT32_MAX); 284 285 if (ao != NULL) { 286 ao->ao_procid = obj->Processor.ProcId; 287 ao->ao_pblklen = obj->Processor.PblkLength; 288 ao->ao_pblkaddr = obj->Processor.PblkAddress; 289 } 290 291 out: 292 if (buf.Pointer != NULL) 293 ACPI_FREE(buf.Pointer); 294 295 return ACPI_FAILURE(rv) ? 1 : 0; 296 } 297 298 static cpuid_t 299 acpicpu_id(uint32_t id) 300 { 301 CPU_INFO_ITERATOR cii; 302 struct cpu_info *ci; 303 304 for (CPU_INFO_FOREACH(cii, ci)) { 305 306 if (id == ci->ci_acpiid) 307 return id; 308 } 309 310 return 0xFFFFFF; 311 } 312 313 static uint32_t 314 acpicpu_cap(struct acpicpu_softc *sc) 315 { 316 uint32_t flags, cap = 0; 317 const char *str; 318 ACPI_STATUS rv; 319 320 /* 321 * Query and set machine-dependent capabilities. 322 * Note that the Intel-specific _PDC method was 323 * deprecated in the ACPI 3.0 in favor of _OSC. 324 */ 325 flags = acpicpu_md_cap(); 326 rv = acpicpu_cap_osc(sc, flags, &cap); 327 328 if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND) { 329 str = "_OSC"; 330 goto fail; 331 } 332 333 rv = acpicpu_cap_pdc(sc, flags); 334 335 if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND) { 336 str = "_PDC"; 337 goto fail; 338 } 339 340 if (cap == 0) 341 cap = flags; 342 343 return cap; 344 345 fail: 346 aprint_error_dev(sc->sc_dev, "failed to evaluate " 347 "%s: %s\n", str, AcpiFormatException(rv)); 348 349 return 0; 350 } 351 352 static ACPI_STATUS 353 acpicpu_cap_pdc(struct acpicpu_softc *sc, uint32_t flags) 354 { 355 ACPI_OBJECT_LIST arg; 356 ACPI_OBJECT obj; 357 uint32_t cap[3]; 358 359 arg.Count = 1; 360 arg.Pointer = &obj; 361 362 cap[0] = ACPICPU_PDC_REVID; 363 cap[1] = 1; 364 cap[2] = flags; 365 366 obj.Type = ACPI_TYPE_BUFFER; 367 obj.Buffer.Length = sizeof(cap); 368 obj.Buffer.Pointer = (void *)cap; 369 370 return AcpiEvaluateObject(sc->sc_node->ad_handle, "_PDC", &arg, NULL); 371 } 372 373 static ACPI_STATUS 374 acpicpu_cap_osc(struct acpicpu_softc *sc, uint32_t flags, uint32_t *val) 375 { 376 ACPI_OBJECT_LIST arg; 377 ACPI_OBJECT obj[4]; 378 ACPI_OBJECT *osc; 379 ACPI_BUFFER buf; 380 ACPI_STATUS rv; 381 uint32_t cap[2]; 382 uint32_t *ptr; 383 int i = 5; 384 385 static uint8_t intel_uuid[16] = { 386 0x16, 0xA6, 0x77, 0x40, 0x0C, 0x29, 0xBE, 0x47, 387 0x9E, 0xBD, 0xD8, 0x70, 0x58, 0x71, 0x39, 0x53 388 }; 389 390 cap[0] = ACPI_OSC_QUERY; 391 cap[1] = flags; 392 393 again: 394 arg.Count = 4; 395 arg.Pointer = obj; 396 397 obj[0].Type = ACPI_TYPE_BUFFER; 398 obj[0].Buffer.Length = sizeof(intel_uuid); 399 obj[0].Buffer.Pointer = intel_uuid; 400 401 obj[1].Type = ACPI_TYPE_INTEGER; 402 obj[1].Integer.Value = ACPICPU_PDC_REVID; 403 404 obj[2].Type = ACPI_TYPE_INTEGER; 405 obj[2].Integer.Value = __arraycount(cap); 406 407 obj[3].Type = ACPI_TYPE_BUFFER; 408 obj[3].Buffer.Length = sizeof(cap); 409 obj[3].Buffer.Pointer = (void *)cap; 410 411 buf.Pointer = NULL; 412 buf.Length = ACPI_ALLOCATE_LOCAL_BUFFER; 413 414 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "_OSC", &arg, &buf); 415 416 if (ACPI_FAILURE(rv)) 417 goto out; 418 419 osc = buf.Pointer; 420 421 if (osc->Type != ACPI_TYPE_BUFFER) { 422 rv = AE_TYPE; 423 goto out; 424 } 425 426 if (osc->Buffer.Length != sizeof(cap)) { 427 rv = AE_BUFFER_OVERFLOW; 428 goto out; 429 } 430 431 ptr = (uint32_t *)osc->Buffer.Pointer; 432 433 if ((ptr[0] & ACPI_OSC_ERROR) != 0) { 434 rv = AE_ERROR; 435 goto out; 436 } 437 438 if ((ptr[0] & (ACPI_OSC_ERROR_REV | ACPI_OSC_ERROR_UUID)) != 0) { 439 rv = AE_BAD_PARAMETER; 440 goto out; 441 } 442 443 /* 444 * "It is strongly recommended that the OS evaluate 445 * _OSC with the Query Support Flag set until _OSC 446 * returns the Capabilities Masked bit clear, to 447 * negotiate the set of features to be granted to 448 * the OS for native support (ACPI 4.0, 6.2.10)." 449 */ 450 if ((ptr[0] & ACPI_OSC_ERROR_MASKED) != 0 && i >= 0) { 451 452 ACPI_FREE(buf.Pointer); 453 i--; 454 455 goto again; 456 } 457 458 if ((cap[0] & ACPI_OSC_QUERY) != 0) { 459 460 ACPI_FREE(buf.Pointer); 461 cap[0] &= ~ACPI_OSC_QUERY; 462 463 goto again; 464 } 465 466 /* 467 * It is permitted for _OSC to return all 468 * bits cleared, but this is specified to 469 * vary on per-device basis. Assume that 470 * everything rather than nothing will be 471 * supported in thise case; we do not need 472 * the firmware to know the CPU features. 473 */ 474 *val = (ptr[1] != 0) ? ptr[1] : cap[1]; 475 476 out: 477 if (buf.Pointer != NULL) 478 ACPI_FREE(buf.Pointer); 479 480 return rv; 481 } 482 483 static void 484 acpicpu_notify(ACPI_HANDLE hdl, uint32_t evt, void *aux) 485 { 486 ACPI_OSD_EXEC_CALLBACK func; 487 struct acpicpu_softc *sc; 488 device_t self = aux; 489 490 sc = device_private(self); 491 492 if (sc->sc_cold != false) 493 return; 494 495 switch (evt) { 496 497 case ACPICPU_C_NOTIFY: 498 499 if ((sc->sc_flags & ACPICPU_FLAG_C) == 0) 500 return; 501 502 func = acpicpu_cstate_callback; 503 break; 504 505 case ACPICPU_P_NOTIFY: 506 507 if ((sc->sc_flags & ACPICPU_FLAG_P) == 0) 508 return; 509 510 func = acpicpu_pstate_callback; 511 break; 512 513 case ACPICPU_T_NOTIFY: 514 515 if ((sc->sc_flags & ACPICPU_FLAG_T) == 0) 516 return; 517 518 func = acpicpu_tstate_callback; 519 break; 520 521 default: 522 aprint_error_dev(sc->sc_dev, "unknown notify: 0x%02X\n", evt); 523 return; 524 } 525 526 (void)AcpiOsExecute(OSL_NOTIFY_HANDLER, func, sc->sc_dev); 527 } 528 529 static bool 530 acpicpu_suspend(device_t self, const pmf_qual_t *qual) 531 { 532 struct acpicpu_softc *sc = device_private(self); 533 534 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0) 535 (void)acpicpu_cstate_suspend(self); 536 537 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0) 538 (void)acpicpu_pstate_suspend(self); 539 540 if ((sc->sc_flags & ACPICPU_FLAG_T) != 0) 541 (void)acpicpu_tstate_suspend(self); 542 543 sc->sc_cold = true; 544 545 return true; 546 } 547 548 static bool 549 acpicpu_resume(device_t self, const pmf_qual_t *qual) 550 { 551 struct acpicpu_softc *sc = device_private(self); 552 553 sc->sc_cold = false; 554 555 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0) 556 (void)acpicpu_cstate_resume(self); 557 558 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0) 559 (void)acpicpu_pstate_resume(self); 560 561 if ((sc->sc_flags & ACPICPU_FLAG_T) != 0) 562 (void)acpicpu_tstate_resume(self); 563 564 return true; 565 } 566 567 #ifdef _MODULE 568 569 MODULE(MODULE_CLASS_DRIVER, acpicpu, NULL); 570 CFDRIVER_DECL(acpicpu, DV_DULL, NULL); 571 572 static int acpicpuloc[] = { -1 }; 573 extern struct cfattach acpicpu_ca; 574 575 static struct cfparent acpiparent = { 576 "acpinodebus", NULL, DVUNIT_ANY 577 }; 578 579 static struct cfdata acpicpu_cfdata[] = { 580 { 581 .cf_name = "acpicpu", 582 .cf_atname = "acpicpu", 583 .cf_unit = 0, 584 .cf_fstate = FSTATE_STAR, 585 .cf_loc = acpicpuloc, 586 .cf_flags = 0, 587 .cf_pspec = &acpiparent, 588 }, 589 590 { NULL, NULL, 0, 0, NULL, 0, NULL } 591 }; 592 593 static int 594 acpicpu_modcmd(modcmd_t cmd, void *context) 595 { 596 int err; 597 598 switch (cmd) { 599 600 case MODULE_CMD_INIT: 601 602 err = config_cfdriver_attach(&acpicpu_cd); 603 604 if (err != 0) 605 return err; 606 607 err = config_cfattach_attach("acpicpu", &acpicpu_ca); 608 609 if (err != 0) { 610 config_cfdriver_detach(&acpicpu_cd); 611 return err; 612 } 613 614 err = config_cfdata_attach(acpicpu_cfdata, 1); 615 616 if (err != 0) { 617 config_cfattach_detach("acpicpu", &acpicpu_ca); 618 config_cfdriver_detach(&acpicpu_cd); 619 return err; 620 } 621 622 return 0; 623 624 case MODULE_CMD_FINI: 625 626 err = config_cfdata_detach(acpicpu_cfdata); 627 628 if (err != 0) 629 return err; 630 631 config_cfattach_detach("acpicpu", &acpicpu_ca); 632 config_cfdriver_detach(&acpicpu_cd); 633 634 return 0; 635 636 default: 637 return ENOTTY; 638 } 639 } 640 641 #endif /* _MODULE */ 642