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 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 /* 27 * ACPI CA OSL for Solaris x86 28 */ 29 30 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 33 #include <sys/types.h> 34 #include <sys/kmem.h> 35 #include <sys/psm.h> 36 #include <sys/pci_cfgspace.h> 37 #include <sys/ddi.h> 38 #include <sys/sunndi.h> 39 #include <sys/pci.h> 40 #include <sys/kobj.h> 41 #include <sys/taskq.h> 42 #include <sys/strlog.h> 43 #include <sys/note.h> 44 45 #include <sys/acpi/acpi.h> 46 #include <sys/acpica.h> 47 48 #define MAX_DAT_FILE_SIZE (64*1024) 49 50 /* local functions */ 51 static int CompressEisaID(char *np); 52 53 static void scan_d2a_map(void); 54 static void scan_d2a_subtree(dev_info_t *dip, ACPI_HANDLE acpiobj, int bus); 55 static void acpica_tag_devinfo(dev_info_t *dip, ACPI_HANDLE acpiobj); 56 57 static int acpica_query_bbn_problem(void); 58 static int acpica_find_pcibus(int busno, ACPI_HANDLE *rh); 59 static int acpica_eval_hid(ACPI_HANDLE dev, char *method, int *rint); 60 static ACPI_STATUS acpica_set_devinfo(ACPI_HANDLE, dev_info_t *); 61 static void acpica_devinfo_handler(ACPI_HANDLE, UINT32, void *); 62 63 /* 64 * Event queue vars 65 */ 66 int acpica_eventq_thread_count = 1; 67 int acpica_eventq_init = 0; 68 ddi_taskq_t *osl_eventq[OSL_EC_BURST_HANDLER+1]; 69 70 /* 71 * Note, if you change this path, you need to update 72 * /boot/grub/filelist.ramdisk and pkg SUNWckr/prototype_i386 73 */ 74 static char *acpi_table_path = "/boot/acpi/tables/"; 75 76 /* non-zero while scan_d2a_map() is working */ 77 static int scanning_d2a_map = 0; 78 static int d2a_done = 0; 79 80 /* set by acpi_poweroff() in PSMs */ 81 int acpica_powering_off = 0; 82 83 /* CPU mapping data */ 84 struct cpu_map_item { 85 MADT_PROCESSOR_APIC *mpa; 86 ACPI_HANDLE obj; 87 }; 88 89 static struct cpu_map_item **cpu_map = NULL; 90 static int cpu_map_count = 0; 91 static int cpu_map_built = 0; 92 93 static int acpi_has_broken_bbn = -1; 94 95 #define D2A_DEBUG 96 97 /* 98 * 99 */ 100 static void 101 discard_event_queues() 102 { 103 int i; 104 105 /* 106 * destroy event queues 107 */ 108 for (i = OSL_GLOBAL_LOCK_HANDLER; i <= OSL_EC_BURST_HANDLER; i++) { 109 if (osl_eventq[i]) 110 ddi_taskq_destroy(osl_eventq[i]); 111 } 112 } 113 114 115 /* 116 * 117 */ 118 static ACPI_STATUS 119 init_event_queues() 120 { 121 char namebuf[32]; 122 int i, error = 0; 123 124 /* 125 * Initialize event queues 126 */ 127 128 for (i = OSL_GLOBAL_LOCK_HANDLER; i <= OSL_EC_BURST_HANDLER; i++) { 129 snprintf(namebuf, 32, "ACPI%d", i); 130 osl_eventq[i] = ddi_taskq_create(NULL, namebuf, 131 acpica_eventq_thread_count, TASKQ_DEFAULTPRI, 0); 132 if (osl_eventq[i] == NULL) 133 error++; 134 } 135 136 if (error != 0) { 137 discard_event_queues(); 138 #ifdef DEBUG 139 cmn_err(CE_WARN, "!acpica: could not initialize event queues"); 140 #endif 141 return (AE_ERROR); 142 } 143 144 acpica_eventq_init = 1; 145 return (AE_OK); 146 } 147 148 /* 149 * 150 */ 151 ACPI_STATUS 152 AcpiOsInitialize(void) 153 { 154 return (AE_OK); 155 } 156 157 /* 158 * 159 */ 160 ACPI_STATUS 161 AcpiOsTerminate(void) 162 { 163 164 discard_event_queues(); 165 return (AE_OK); 166 } 167 168 169 ACPI_STATUS 170 AcpiOsGetRootPointer(UINT32 Flags, ACPI_POINTER *Address) 171 { 172 uint_t acpi_root_tab; 173 174 /* 175 * For EFI firmware, the root pointer is defined in EFI systab. 176 * The boot code process the table and put the physical address 177 * in the acpi-root-tab property. 178 */ 179 acpi_root_tab = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_root_node(), 0, 180 "acpi-root-tab", 0); 181 if (acpi_root_tab != 0) { 182 Address->PointerType = ACPI_PHYSICAL_POINTER; 183 Address->Pointer.Physical = acpi_root_tab; 184 return (AE_OK); 185 } 186 return (AcpiFindRootPointer(Flags, Address)); 187 } 188 189 /*ARGSUSED*/ 190 ACPI_STATUS 191 AcpiOsPredefinedOverride(const ACPI_PREDEFINED_NAMES *InitVal, 192 ACPI_STRING *NewVal) 193 { 194 195 *NewVal = 0; 196 return (AE_OK); 197 } 198 199 static void 200 acpica_strncpy(char *dest, const char *src, int len) 201 { 202 203 /*LINTED*/ 204 while ((*dest++ = *src++) && (--len > 0)) 205 /* copy the string */; 206 *dest = '\0'; 207 } 208 209 ACPI_STATUS 210 AcpiOsTableOverride(ACPI_TABLE_HEADER *ExistingTable, 211 ACPI_TABLE_HEADER **NewTable) 212 { 213 char signature[5]; 214 char oemid[7]; 215 char oemtableid[9]; 216 struct _buf *file; 217 char *buf1, *buf2; 218 int count; 219 char acpi_table_loc[128]; 220 221 acpica_strncpy(signature, ExistingTable->Signature, 4); 222 acpica_strncpy(oemid, ExistingTable->OemId, 6); 223 acpica_strncpy(oemtableid, ExistingTable->OemTableId, 8); 224 225 #ifdef DEBUG 226 cmn_err(CE_NOTE, "!acpica: table [%s] v%d OEM ID [%s]" 227 " OEM TABLE ID [%s] OEM rev %x", 228 signature, ExistingTable->Revision, oemid, oemtableid, 229 ExistingTable->OemRevision); 230 #endif 231 232 /* File name format is "signature_oemid_oemtableid.dat" */ 233 (void) strcpy(acpi_table_loc, acpi_table_path); 234 (void) strcat(acpi_table_loc, signature); /* for example, DSDT */ 235 (void) strcat(acpi_table_loc, "_"); 236 (void) strcat(acpi_table_loc, oemid); /* for example, IntelR */ 237 (void) strcat(acpi_table_loc, "_"); 238 (void) strcat(acpi_table_loc, oemtableid); /* for example, AWRDACPI */ 239 (void) strcat(acpi_table_loc, ".dat"); 240 241 file = kobj_open_file(acpi_table_loc); 242 if (file == (struct _buf *)-1) { 243 *NewTable = 0; 244 return (AE_OK); 245 } else { 246 buf1 = (char *)kmem_alloc(MAX_DAT_FILE_SIZE, KM_SLEEP); 247 count = kobj_read_file(file, buf1, MAX_DAT_FILE_SIZE-1, 0); 248 if (count >= MAX_DAT_FILE_SIZE) { 249 cmn_err(CE_WARN, "!acpica: table %s file size too big", 250 acpi_table_loc); 251 *NewTable = 0; 252 } else { 253 buf2 = (char *)kmem_alloc(count, KM_SLEEP); 254 (void) memcpy(buf2, buf1, count); 255 *NewTable = (ACPI_TABLE_HEADER *)buf2; 256 cmn_err(CE_NOTE, "!acpica: replacing table: %s", 257 acpi_table_loc); 258 } 259 } 260 kobj_close_file(file); 261 kmem_free(buf1, MAX_DAT_FILE_SIZE); 262 263 return (AE_OK); 264 } 265 266 267 /* 268 * ACPI semaphore implementation 269 */ 270 typedef struct { 271 kmutex_t mutex; 272 kcondvar_t cv; 273 uint32_t available; 274 uint32_t initial; 275 uint32_t maximum; 276 } acpi_sema_t; 277 278 /* 279 * 280 */ 281 void 282 acpi_sema_init(acpi_sema_t *sp, unsigned max, unsigned count) 283 { 284 mutex_init(&sp->mutex, NULL, MUTEX_DRIVER, NULL); 285 cv_init(&sp->cv, NULL, CV_DRIVER, NULL); 286 /* no need to enter mutex here at creation */ 287 sp->available = count; 288 sp->initial = count; 289 sp->maximum = max; 290 } 291 292 /* 293 * 294 */ 295 void 296 acpi_sema_destroy(acpi_sema_t *sp) 297 { 298 299 cv_destroy(&sp->cv); 300 mutex_destroy(&sp->mutex); 301 } 302 303 /* 304 * 305 */ 306 ACPI_STATUS 307 acpi_sema_p(acpi_sema_t *sp, unsigned count, uint16_t wait_time) 308 { 309 ACPI_STATUS rv = AE_OK; 310 clock_t deadline; 311 312 mutex_enter(&sp->mutex); 313 314 if (sp->available >= count) { 315 /* 316 * Enough units available, no blocking 317 */ 318 sp->available -= count; 319 mutex_exit(&sp->mutex); 320 return (rv); 321 } else if (wait_time == 0) { 322 /* 323 * Not enough units available and timeout 324 * specifies no blocking 325 */ 326 rv = AE_TIME; 327 mutex_exit(&sp->mutex); 328 return (rv); 329 } 330 331 /* 332 * Not enough units available and timeout specifies waiting 333 */ 334 if (wait_time != ACPI_WAIT_FOREVER) 335 deadline = ddi_get_lbolt() + 336 (clock_t)drv_usectohz(wait_time * 1000); 337 338 do { 339 if (wait_time == ACPI_WAIT_FOREVER) 340 cv_wait(&sp->cv, &sp->mutex); 341 else if (cv_timedwait(&sp->cv, &sp->mutex, deadline) < 0) { 342 rv = AE_TIME; 343 break; 344 } 345 } while (sp->available < count); 346 347 /* if we dropped out of the wait with AE_OK, we got the units */ 348 if (rv == AE_OK) 349 sp->available -= count; 350 351 mutex_exit(&sp->mutex); 352 return (rv); 353 } 354 355 /* 356 * 357 */ 358 void 359 acpi_sema_v(acpi_sema_t *sp, unsigned count) 360 { 361 mutex_enter(&sp->mutex); 362 sp->available += count; 363 cv_broadcast(&sp->cv); 364 mutex_exit(&sp->mutex); 365 } 366 367 368 ACPI_STATUS 369 AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, 370 ACPI_HANDLE *OutHandle) 371 { 372 acpi_sema_t *sp; 373 374 if ((OutHandle == NULL) || (InitialUnits > MaxUnits)) 375 return (AE_BAD_PARAMETER); 376 377 sp = (acpi_sema_t *)kmem_alloc(sizeof (acpi_sema_t), KM_SLEEP); 378 acpi_sema_init(sp, MaxUnits, InitialUnits); 379 *OutHandle = (ACPI_HANDLE)sp; 380 return (AE_OK); 381 } 382 383 384 ACPI_STATUS 385 AcpiOsDeleteSemaphore(ACPI_HANDLE Handle) 386 { 387 388 if (Handle == NULL) 389 return (AE_BAD_PARAMETER); 390 391 acpi_sema_destroy((acpi_sema_t *)Handle); 392 kmem_free((void *)Handle, sizeof (acpi_sema_t)); 393 return (AE_OK); 394 } 395 396 ACPI_STATUS 397 AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT16 Timeout) 398 { 399 400 if ((Handle == NULL) || (Units < 1)) 401 return (AE_BAD_PARAMETER); 402 403 return (acpi_sema_p((acpi_sema_t *)Handle, Units, Timeout)); 404 } 405 406 ACPI_STATUS 407 AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units) 408 { 409 410 if ((Handle == NULL) || (Units < 1)) 411 return (AE_BAD_PARAMETER); 412 413 acpi_sema_v((acpi_sema_t *)Handle, Units); 414 return (AE_OK); 415 } 416 417 ACPI_STATUS 418 AcpiOsCreateLock(ACPI_HANDLE *OutHandle) 419 { 420 kmutex_t *mp; 421 422 if (OutHandle == NULL) 423 return (AE_BAD_PARAMETER); 424 425 mp = (kmutex_t *)kmem_alloc(sizeof (kmutex_t), KM_SLEEP); 426 mutex_init(mp, NULL, MUTEX_DRIVER, NULL); 427 *OutHandle = (ACPI_HANDLE)mp; 428 return (AE_OK); 429 } 430 431 void 432 AcpiOsDeleteLock(ACPI_HANDLE Handle) 433 { 434 435 if (Handle == NULL) 436 return; 437 438 mutex_destroy((kmutex_t *)Handle); 439 kmem_free((void *)Handle, sizeof (kmutex_t)); 440 } 441 442 ACPI_NATIVE_UINT 443 AcpiOsAcquireLock(ACPI_HANDLE Handle) 444 { 445 446 if (Handle == NULL) 447 return (AE_BAD_PARAMETER); 448 449 mutex_enter((kmutex_t *)Handle); 450 return (AE_OK); 451 } 452 453 void 454 AcpiOsReleaseLock(ACPI_HANDLE Handle, ACPI_NATIVE_UINT Flags) 455 { 456 _NOTE(ARGUNUSED(Flags)) 457 458 if (Handle == NULL) 459 return; 460 461 mutex_exit((kmutex_t *)Handle); 462 } 463 464 465 void * 466 AcpiOsAllocate(ACPI_SIZE Size) 467 { 468 ACPI_SIZE *tmp_ptr; 469 470 Size += sizeof (Size); 471 tmp_ptr = (ACPI_SIZE *)kmem_zalloc(Size, KM_SLEEP); 472 *tmp_ptr++ = Size; 473 return (tmp_ptr); 474 } 475 476 void 477 AcpiOsFree(void *Memory) 478 { 479 ACPI_SIZE size, *tmp_ptr; 480 481 tmp_ptr = (ACPI_SIZE *)Memory; 482 tmp_ptr -= 1; 483 size = *tmp_ptr; 484 kmem_free(tmp_ptr, size); 485 } 486 487 ACPI_STATUS 488 AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress, 489 ACPI_SIZE Size, void **LogicalAddress) 490 { 491 /* FUTUREWORK: test PhysicalAddress for > 32 bits */ 492 *LogicalAddress = psm_map_new((paddr_t)PhysicalAddress, 493 (size_t)Size, PSM_PROT_WRITE | PSM_PROT_READ); 494 495 return (*LogicalAddress == NULL ? AE_NO_MEMORY : AE_OK); 496 } 497 498 void 499 AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Size) 500 { 501 502 psm_unmap((caddr_t)LogicalAddress, (size_t)Size); 503 } 504 505 /*ARGSUSED*/ 506 ACPI_STATUS 507 AcpiOsGetPhysicalAddress(void *LogicalAddress, 508 ACPI_PHYSICAL_ADDRESS *PhysicalAddress) 509 { 510 511 /* UNIMPLEMENTED: not invoked by ACPI CA code */ 512 return (AE_NOT_IMPLEMENTED); 513 } 514 515 516 ACPI_OSD_HANDLER acpi_isr; 517 void *acpi_isr_context; 518 519 uint_t 520 acpi_wrapper_isr(char *arg) 521 { 522 _NOTE(ARGUNUSED(arg)) 523 524 int status; 525 526 status = (*acpi_isr)(acpi_isr_context); 527 528 if (status == ACPI_INTERRUPT_HANDLED) { 529 return (DDI_INTR_CLAIMED); 530 } else { 531 return (DDI_INTR_UNCLAIMED); 532 } 533 } 534 535 static int acpi_intr_hooked = 0; 536 537 ACPI_STATUS 538 AcpiOsInstallInterruptHandler(UINT32 InterruptNumber, 539 ACPI_OSD_HANDLER ServiceRoutine, 540 void *Context) 541 { 542 _NOTE(ARGUNUSED(InterruptNumber)) 543 544 int retval; 545 int sci_vect; 546 iflag_t sci_flags; 547 548 acpi_isr = ServiceRoutine; 549 acpi_isr_context = Context; 550 551 /* 552 * Get SCI (adjusted for PIC/APIC mode if necessary) 553 */ 554 if (acpica_get_sci(&sci_vect, &sci_flags) != AE_OK) { 555 return (AE_ERROR); 556 } 557 558 #ifdef DEBUG 559 cmn_err(CE_NOTE, "!acpica: attaching SCI %d", sci_vect); 560 #endif 561 562 retval = add_avintr(NULL, SCI_IPL, (avfunc)acpi_wrapper_isr, 563 "ACPI SCI", sci_vect, NULL, NULL, NULL, NULL); 564 if (retval) { 565 acpi_intr_hooked = 1; 566 return (AE_OK); 567 } else 568 return (AE_BAD_PARAMETER); 569 } 570 571 ACPI_STATUS 572 AcpiOsRemoveInterruptHandler(UINT32 InterruptNumber, 573 ACPI_OSD_HANDLER ServiceRoutine) 574 { 575 _NOTE(ARGUNUSED(ServiceRoutine)) 576 577 #ifdef DEBUG 578 cmn_err(CE_NOTE, "!acpica: detaching SCI %d", InterruptNumber); 579 #endif 580 if (acpi_intr_hooked) { 581 rem_avintr(NULL, LOCK_LEVEL - 1, (avfunc)acpi_wrapper_isr, 582 InterruptNumber); 583 acpi_intr_hooked = 0; 584 } 585 return (AE_OK); 586 } 587 588 589 ACPI_THREAD_ID 590 AcpiOsGetThreadId(void) 591 { 592 /* 593 * ACPI CA regards thread ID as an error, but it's valid 594 * on Solaris during kernel initialization. Thus, 1 is added 595 * to the kernel thread ID to avoid returning 0 596 */ 597 return (ddi_get_kt_did() + 1); 598 } 599 600 /* 601 * 602 */ 603 ACPI_STATUS 604 AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function, 605 void *Context) 606 { 607 608 if (!acpica_eventq_init) { 609 /* 610 * Create taskqs for event handling 611 */ 612 if (init_event_queues() != AE_OK) 613 return (AE_ERROR); 614 } 615 616 if (ddi_taskq_dispatch(osl_eventq[Type], Function, Context, 617 DDI_NOSLEEP) == DDI_FAILURE) { 618 #ifdef DEBUG 619 cmn_err(CE_WARN, "!acpica: unable to dispatch event"); 620 #endif 621 return (AE_ERROR); 622 } 623 return (AE_OK); 624 625 } 626 627 void 628 AcpiOsSleep(ACPI_INTEGER Milliseconds) 629 { 630 /* 631 * During kernel startup, before the first 632 * tick interrupt has taken place, we can't call 633 * delay; very late in kernel shutdown, clock interrupts 634 * are blocked, so delay doesn't work then either. 635 * So we busy wait if lbolt == 0 (kernel startup) 636 * or if psm_shutdown() has set acpi_powering_off to 637 * a non-zero value. 638 */ 639 if ((ddi_get_lbolt() == 0) || acpica_powering_off) 640 drv_usecwait(Milliseconds * 1000); 641 else 642 delay(drv_usectohz(Milliseconds * 1000)); 643 } 644 645 void 646 AcpiOsStall(UINT32 Microseconds) 647 { 648 drv_usecwait(Microseconds); 649 } 650 651 652 /* 653 * Implementation of "Windows 2001" compatible I/O permission map 654 * 655 */ 656 #define OSL_IO_NONE (0) 657 #define OSL_IO_READ (1<<0) 658 #define OSL_IO_WRITE (1<<1) 659 #define OSL_IO_RW (OSL_IO_READ | OSL_IO_WRITE) 660 #define OSL_IO_TERM (1<<2) 661 #define OSL_IO_DEFAULT OSL_IO_RW 662 663 static struct io_perm { 664 ACPI_IO_ADDRESS low; 665 ACPI_IO_ADDRESS high; 666 uint8_t perm; 667 } osl_io_perm[] = { 668 { 0xcf8, 0xd00, OSL_IO_NONE | OSL_IO_TERM } 669 }; 670 671 672 /* 673 * 674 */ 675 static struct io_perm * 676 osl_io_find_perm(ACPI_IO_ADDRESS addr) 677 { 678 struct io_perm *p; 679 680 p = osl_io_perm; 681 while (p != NULL) { 682 if ((p->low <= addr) && (addr <= p->high)) 683 break; 684 p = (p->perm & OSL_IO_TERM) ? NULL : p+1; 685 } 686 687 return (p); 688 } 689 690 /* 691 * 692 */ 693 ACPI_STATUS 694 AcpiOsReadPort(ACPI_IO_ADDRESS Address, UINT32 *Value, UINT32 Width) 695 { 696 struct io_perm *p; 697 698 /* verify permission */ 699 p = osl_io_find_perm(Address); 700 if (p && (p->perm & OSL_IO_READ) == 0) { 701 cmn_err(CE_WARN, "!AcpiOsReadPort: %lx %u not permitted", 702 (long)Address, Width); 703 *Value = 0xffffffff; 704 return (AE_ERROR); 705 } 706 707 switch (Width) { 708 case 8: 709 *Value = inb(Address); 710 break; 711 case 16: 712 *Value = inw(Address); 713 break; 714 case 32: 715 *Value = inl(Address); 716 break; 717 default: 718 cmn_err(CE_WARN, "!AcpiOsReadPort: %lx %u failed", 719 (long)Address, Width); 720 return (AE_BAD_PARAMETER); 721 } 722 return (AE_OK); 723 } 724 725 ACPI_STATUS 726 AcpiOsWritePort(ACPI_IO_ADDRESS Address, UINT32 Value, UINT32 Width) 727 { 728 struct io_perm *p; 729 730 /* verify permission */ 731 p = osl_io_find_perm(Address); 732 if (p && (p->perm & OSL_IO_WRITE) == 0) { 733 cmn_err(CE_WARN, "!AcpiOsWritePort: %lx %u not permitted", 734 (long)Address, Width); 735 return (AE_ERROR); 736 } 737 738 switch (Width) { 739 case 8: 740 outb(Address, Value); 741 break; 742 case 16: 743 outw(Address, Value); 744 break; 745 case 32: 746 outl(Address, Value); 747 break; 748 default: 749 cmn_err(CE_WARN, "!AcpiOsWritePort: %lx %u failed", 750 (long)Address, Width); 751 return (AE_BAD_PARAMETER); 752 } 753 return (AE_OK); 754 } 755 756 757 /* 758 * 759 */ 760 761 #define OSL_RW(ptr, val, type, rw) \ 762 { if (rw) *((type *)(ptr)) = *((type *) val); \ 763 else *((type *) val) = *((type *)(ptr)); } 764 765 766 static void 767 osl_rw_memory(ACPI_PHYSICAL_ADDRESS Address, UINT32 *Value, 768 UINT32 Width, int write) 769 { 770 size_t maplen = Width / 8; 771 caddr_t ptr; 772 773 ptr = psm_map_new((paddr_t)Address, maplen, 774 PSM_PROT_WRITE | PSM_PROT_READ); 775 776 switch (maplen) { 777 case 1: 778 OSL_RW(ptr, Value, uint8_t, write); 779 break; 780 case 2: 781 OSL_RW(ptr, Value, uint16_t, write); 782 break; 783 case 4: 784 OSL_RW(ptr, Value, uint32_t, write); 785 break; 786 default: 787 cmn_err(CE_WARN, "!osl_rw_memory: invalid size %d", 788 Width); 789 break; 790 } 791 792 psm_unmap(ptr, maplen); 793 } 794 795 ACPI_STATUS 796 AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, 797 UINT32 *Value, UINT32 Width) 798 { 799 osl_rw_memory(Address, Value, Width, 0); 800 return (AE_OK); 801 } 802 803 ACPI_STATUS 804 AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, 805 UINT32 Value, UINT32 Width) 806 { 807 osl_rw_memory(Address, &Value, Width, 1); 808 return (AE_OK); 809 } 810 811 812 ACPI_STATUS 813 AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, 814 void *Value, UINT32 Width) 815 { 816 817 switch (Width) { 818 case 8: 819 *((UINT64 *)Value) = (UINT64)(*pci_getb_func) 820 (PciId->Bus, PciId->Device, PciId->Function, Register); 821 break; 822 case 16: 823 *((UINT64 *)Value) = (UINT64)(*pci_getw_func) 824 (PciId->Bus, PciId->Device, PciId->Function, Register); 825 break; 826 case 32: 827 *((UINT64 *)Value) = (UINT64)(*pci_getl_func) 828 (PciId->Bus, PciId->Device, PciId->Function, Register); 829 break; 830 case 64: 831 default: 832 cmn_err(CE_WARN, "!AcpiOsReadPciConfiguration: %x %u failed", 833 Register, Width); 834 return (AE_BAD_PARAMETER); 835 } 836 return (AE_OK); 837 } 838 839 /* 840 * 841 */ 842 int acpica_write_pci_config_ok = 1; 843 844 ACPI_STATUS 845 AcpiOsWritePciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, 846 ACPI_INTEGER Value, UINT32 Width) 847 { 848 849 if (!acpica_write_pci_config_ok) { 850 cmn_err(CE_NOTE, "!write to PCI cfg %x/%x/%x %x" 851 " %lx %d not permitted", PciId->Bus, PciId->Device, 852 PciId->Function, Register, (long)Value, Width); 853 return (AE_OK); 854 } 855 856 switch (Width) { 857 case 8: 858 (*pci_putb_func)(PciId->Bus, PciId->Device, PciId->Function, 859 Register, (uint8_t)Value); 860 break; 861 case 16: 862 (*pci_putw_func)(PciId->Bus, PciId->Device, PciId->Function, 863 Register, (uint16_t)Value); 864 break; 865 case 32: 866 (*pci_putl_func)(PciId->Bus, PciId->Device, PciId->Function, 867 Register, (uint32_t)Value); 868 break; 869 case 64: 870 default: 871 cmn_err(CE_WARN, "!AcpiOsWritePciConfiguration: %x %u failed", 872 Register, Width); 873 return (AE_BAD_PARAMETER); 874 } 875 return (AE_OK); 876 } 877 878 /* 879 * Called with ACPI_HANDLEs for both a PCI Config Space 880 * OpRegion and (what ACPI CA thinks is) the PCI device 881 * to which this ConfigSpace OpRegion belongs. Since 882 * ACPI CA depends on a valid _BBN object being present 883 * and this is not always true (one old x86 had broken _BBN), 884 * we go ahead and get the correct PCI bus number using the 885 * devinfo mapping (which compensates for broken _BBN). 886 * 887 * Default values for bus, segment, device and function are 888 * all 0 when ACPI CA can't figure them out. 889 * 890 * Some BIOSes implement _BBN() by reading PCI config space 891 * on bus #0 - which means that we'll recurse when we attempt 892 * to create the devinfo-to-ACPI map. If Derive is called during 893 * scan_d2a_map, we don't translate the bus # and return. 894 * 895 * We get the parent of the OpRegion, which must be a PCI 896 * node, fetch the associated devinfo node and snag the 897 * b/d/f from it. 898 */ 899 void 900 AcpiOsDerivePciId(ACPI_HANDLE rhandle, ACPI_HANDLE chandle, 901 ACPI_PCI_ID **PciId) 902 { 903 ACPI_HANDLE handle; 904 dev_info_t *dip; 905 int bus, device, func, devfn; 906 907 908 /* 909 * See above - avoid recursing during scanning_d2a_map. 910 */ 911 if (scanning_d2a_map) 912 return; 913 914 /* 915 * Get the OpRegion's parent 916 */ 917 if (AcpiGetParent(chandle, &handle) != AE_OK) 918 return; 919 920 /* 921 * If we've mapped the ACPI node to the devinfo 922 * tree, use the devinfo reg property 923 */ 924 if (acpica_get_devinfo(handle, &dip) == AE_OK) { 925 (void) acpica_get_bdf(dip, &bus, &device, &func); 926 (*PciId)->Bus = bus; 927 (*PciId)->Device = device; 928 (*PciId)->Function = func; 929 } else if (acpica_eval_int(handle, "_ADR", &devfn) == AE_OK) { 930 /* no devinfo node - just confirm the d/f */ 931 (*PciId)->Device = (devfn >> 16) & 0xFFFF; 932 (*PciId)->Function = devfn & 0xFFFF; 933 } 934 } 935 936 937 /*ARGSUSED*/ 938 BOOLEAN 939 AcpiOsReadable(void *Pointer, ACPI_SIZE Length) 940 { 941 942 /* Always says yes; all mapped memory assumed readable */ 943 return (1); 944 } 945 946 /*ARGSUSED*/ 947 BOOLEAN 948 AcpiOsWritable(void *Pointer, ACPI_SIZE Length) 949 { 950 951 /* Always says yes; all mapped memory assumed writable */ 952 return (1); 953 } 954 955 UINT64 956 AcpiOsGetTimer(void) 957 { 958 /* gethrtime() returns 1nS resolution; convert to 100nS granules */ 959 return ((gethrtime() + 50) / 100); 960 } 961 962 /*ARGSUSED*/ 963 ACPI_STATUS 964 AcpiOsValidateInterface(char *interface) 965 { 966 return (AE_SUPPORT); 967 } 968 969 /*ARGSUSED*/ 970 ACPI_STATUS 971 AcpiOsValidateAddress(UINT8 spaceid, ACPI_PHYSICAL_ADDRESS addr, 972 ACPI_SIZE length) 973 { 974 return (AE_OK); 975 } 976 977 ACPI_STATUS 978 AcpiOsSignal(UINT32 Function, void *Info) 979 { 980 _NOTE(ARGUNUSED(Function, Info)) 981 982 /* FUTUREWORK: debugger support */ 983 984 cmn_err(CE_NOTE, "!OsSignal unimplemented"); 985 return (AE_OK); 986 } 987 988 void ACPI_INTERNAL_VAR_XFACE 989 AcpiOsPrintf(const char *Format, ...) 990 { 991 va_list ap; 992 993 va_start(ap, Format); 994 AcpiOsVprintf(Format, ap); 995 va_end(ap); 996 } 997 998 /* 999 * When != 0, sends output to console 1000 * Patchable with kmdb or /etc/system. 1001 */ 1002 int acpica_console_out = 0; 1003 1004 #define ACPICA_OUTBUF_LEN 160 1005 char acpica_outbuf[ACPICA_OUTBUF_LEN]; 1006 int acpica_outbuf_offset; 1007 1008 /* 1009 * 1010 */ 1011 static void 1012 acpica_pr_buf(char *buf, int buflen) 1013 { 1014 char c, *bufp, *outp; 1015 int out_remaining; 1016 1017 /* 1018 * copy the supplied buffer into the output buffer 1019 * when we hit a '\n' or overflow the output buffer, 1020 * output and reset the output buffer 1021 */ 1022 bufp = buf; 1023 outp = acpica_outbuf + acpica_outbuf_offset; 1024 out_remaining = ACPICA_OUTBUF_LEN - acpica_outbuf_offset - 1; 1025 while (c = *bufp++) { 1026 *outp++ = c; 1027 if (c == '\n' || --out_remaining == 0) { 1028 *outp = '\0'; 1029 if (acpica_console_out) 1030 printf(acpica_outbuf); 1031 else 1032 (void) strlog(0, 0, 0, 1033 SL_CONSOLE | SL_NOTE | SL_LOGONLY, 1034 acpica_outbuf); 1035 acpica_outbuf_offset = 0; 1036 outp = acpica_outbuf; 1037 out_remaining = ACPICA_OUTBUF_LEN - 1; 1038 } 1039 } 1040 1041 acpica_outbuf_offset = outp - acpica_outbuf; 1042 kmem_free(buf, buflen); 1043 } 1044 1045 void 1046 AcpiOsVprintf(const char *Format, va_list Args) 1047 { 1048 va_list save; 1049 int buflen; 1050 char *buf; 1051 1052 /* 1053 * Try to be nice and emit the message via strlog(). 1054 * Unfortunately, vstrlog() doesn't define the format 1055 * string as const char, so we allocate a local buffer 1056 * use vsnprintf(). 1057 * 1058 * If we fail to allocate a string buffer, we resort 1059 * to printf(). 1060 */ 1061 va_copy(save, Args); 1062 buflen = vsnprintf(NULL, 0, Format, save) + 1; 1063 buf = kmem_alloc(buflen, KM_NOSLEEP); 1064 if (buf == NULL) { 1065 vprintf(Format, Args); 1066 return; 1067 } 1068 (void) vsnprintf(buf, buflen, Format, Args); 1069 acpica_pr_buf(buf, buflen); 1070 } 1071 1072 void 1073 AcpiOsRedirectOutput(void *Destination) 1074 { 1075 _NOTE(ARGUNUSED(Destination)) 1076 1077 /* FUTUREWORK: debugger support */ 1078 1079 #ifdef DEBUG 1080 cmn_err(CE_WARN, "!acpica: AcpiOsRedirectOutput called"); 1081 #endif 1082 } 1083 1084 1085 UINT32 1086 AcpiOsGetLine(char *Buffer) 1087 { 1088 _NOTE(ARGUNUSED(Buffer)) 1089 1090 /* FUTUREWORK: debugger support */ 1091 1092 return (0); 1093 } 1094 1095 1096 1097 1098 /* 1099 * Device tree binding 1100 */ 1101 1102 static int 1103 acpica_find_pcibus(int busno, ACPI_HANDLE *rh) 1104 { 1105 ACPI_HANDLE sbobj, busobj; 1106 int hid, bbn; 1107 1108 /* initialize static flag by querying ACPI namespace for bug */ 1109 if (acpi_has_broken_bbn == -1) 1110 acpi_has_broken_bbn = acpica_query_bbn_problem(); 1111 1112 busobj = NULL; 1113 AcpiGetHandle(NULL, "\\_SB", &sbobj); 1114 while (AcpiGetNextObject(ACPI_TYPE_DEVICE, sbobj, busobj, 1115 &busobj) == AE_OK) { 1116 if (acpica_eval_hid(busobj, "_HID", &hid) == AE_OK && 1117 (hid == HID_PCI_BUS || hid == HID_PCI_EXPRESS_BUS)) { 1118 if (acpi_has_broken_bbn) { 1119 ACPI_BUFFER rb; 1120 rb.Pointer = NULL; 1121 rb.Length = ACPI_ALLOCATE_BUFFER; 1122 1123 /* Decree _BBN == n from PCI<n> */ 1124 if (AcpiGetName(busobj, ACPI_SINGLE_NAME, &rb) 1125 != AE_OK) { 1126 return (AE_ERROR); 1127 } 1128 bbn = ((char *)rb.Pointer)[3] - '0'; 1129 AcpiOsFree(rb.Pointer); 1130 if (bbn == busno || busno == 0) { 1131 *rh = busobj; 1132 return (AE_OK); 1133 } 1134 } else { 1135 if (acpica_eval_int(busobj, "_BBN", &bbn) == 1136 AE_OK) { 1137 if (bbn == busno) { 1138 *rh = busobj; 1139 return (AE_OK); 1140 } 1141 } else if (busno == 0) { 1142 *rh = busobj; 1143 return (AE_OK); 1144 } 1145 } 1146 } 1147 } 1148 return (AE_ERROR); 1149 } 1150 1151 1152 /* 1153 * Look for ACPI problem where _BBN is zero for multiple PCI buses 1154 * This is a clear ACPI bug, but we have a workaround in acpica_find_pcibus() 1155 * below if it exists. 1156 */ 1157 static int 1158 acpica_query_bbn_problem(void) 1159 { 1160 ACPI_HANDLE sbobj, busobj; 1161 int hid, bbn; 1162 int zerobbncnt; 1163 1164 busobj = NULL; 1165 zerobbncnt = 0; 1166 1167 AcpiGetHandle(NULL, "\\_SB", &sbobj); 1168 1169 while (AcpiGetNextObject(ACPI_TYPE_DEVICE, sbobj, busobj, 1170 &busobj) == AE_OK) { 1171 if ((acpica_eval_hid(busobj, "_HID", &hid) == AE_OK) && 1172 (hid == HID_PCI_BUS || hid == HID_PCI_EXPRESS_BUS) && 1173 (acpica_eval_int(busobj, "_BBN", &bbn) == AE_OK)) { 1174 if (bbn == 0) { 1175 /* 1176 * If we find more than one bus with a 0 _BBN 1177 * we have the problem that BigBear's BIOS shows 1178 */ 1179 if (++zerobbncnt > 1) 1180 return (1); 1181 } 1182 } 1183 } 1184 return (0); 1185 } 1186 1187 static const char hextab[] = "0123456789ABCDEF"; 1188 1189 static int 1190 hexdig(int c) 1191 { 1192 /* 1193 * Get hex digit: 1194 * 1195 * Returns the 4-bit hex digit named by the input character. Returns 1196 * zero if the input character is not valid hex! 1197 */ 1198 1199 int x = ((c < 'a') || (c > 'z')) ? c : (c - ' '); 1200 int j = sizeof (hextab); 1201 1202 while (--j && (x != hextab[j])) { 1203 } 1204 return (j); 1205 } 1206 1207 static int 1208 CompressEisaID(char *np) 1209 { 1210 /* 1211 * Compress an EISA device name: 1212 * 1213 * This routine converts a 7-byte ASCII device name into the 4-byte 1214 * compressed form used by EISA (50 bytes of ROM to save 1 byte of 1215 * NV-RAM!) 1216 */ 1217 1218 union { char octets[4]; int retval; } myu; 1219 1220 myu.octets[0] = ((np[0] & 0x1F) << 2) + ((np[1] >> 3) & 0x03); 1221 myu.octets[1] = ((np[1] & 0x07) << 5) + (np[2] & 0x1F); 1222 myu.octets[2] = (hexdig(np[3]) << 4) + hexdig(np[4]); 1223 myu.octets[3] = (hexdig(np[5]) << 4) + hexdig(np[6]); 1224 1225 return (myu.retval); 1226 } 1227 1228 ACPI_STATUS 1229 acpica_eval_int(ACPI_HANDLE dev, char *method, int *rint) 1230 { 1231 ACPI_STATUS status; 1232 ACPI_BUFFER rb; 1233 ACPI_OBJECT ro; 1234 1235 rb.Pointer = &ro; 1236 rb.Length = sizeof (ro); 1237 if ((status = AcpiEvaluateObjectTyped(dev, method, NULL, &rb, 1238 ACPI_TYPE_INTEGER)) == AE_OK) 1239 *rint = ro.Integer.Value; 1240 1241 return (status); 1242 } 1243 1244 static int 1245 acpica_eval_hid(ACPI_HANDLE dev, char *method, int *rint) 1246 { 1247 ACPI_BUFFER rb; 1248 ACPI_OBJECT *rv; 1249 1250 rb.Pointer = NULL; 1251 rb.Length = ACPI_ALLOCATE_BUFFER; 1252 if (AcpiEvaluateObject(dev, method, NULL, &rb) == AE_OK) { 1253 rv = rb.Pointer; 1254 if (rv->Type == ACPI_TYPE_INTEGER) { 1255 *rint = rv->Integer.Value; 1256 AcpiOsFree(rv); 1257 return (AE_OK); 1258 } else if (rv->Type == ACPI_TYPE_STRING) { 1259 char *stringData; 1260 1261 /* Convert the string into an EISA ID */ 1262 if (rv->String.Pointer == NULL) { 1263 AcpiOsFree(rv); 1264 return (AE_ERROR); 1265 } 1266 1267 stringData = rv->String.Pointer; 1268 1269 /* 1270 * If the string is an EisaID, it must be 7 1271 * characters; if it's an ACPI ID, it will be 8 1272 * (and we don't care about ACPI ids here). 1273 */ 1274 if (strlen(stringData) != 7) { 1275 AcpiOsFree(rv); 1276 return (AE_ERROR); 1277 } 1278 1279 *rint = CompressEisaID(stringData); 1280 AcpiOsFree(rv); 1281 return (AE_OK); 1282 } else 1283 AcpiOsFree(rv); 1284 } 1285 return (AE_ERROR); 1286 } 1287 1288 /* 1289 * Create linkage between devinfo nodes and ACPI nodes 1290 */ 1291 static void 1292 acpica_tag_devinfo(dev_info_t *dip, ACPI_HANDLE acpiobj) 1293 { 1294 ACPI_STATUS status; 1295 ACPI_BUFFER rb; 1296 1297 /* 1298 * Tag the ACPI node with the dip 1299 */ 1300 status = acpica_set_devinfo(acpiobj, dip); 1301 ASSERT(status == AE_OK); 1302 1303 /* 1304 * Tag the devinfo node with the ACPI name 1305 */ 1306 rb.Pointer = NULL; 1307 rb.Length = ACPI_ALLOCATE_BUFFER; 1308 if (AcpiGetName(acpiobj, ACPI_FULL_PATHNAME, &rb) == AE_OK) { 1309 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 1310 "acpi-namespace", (char *)rb.Pointer); 1311 AcpiOsFree(rb.Pointer); 1312 } else { 1313 cmn_err(CE_WARN, "acpica: could not get ACPI path!"); 1314 } 1315 } 1316 1317 static void 1318 acpica_add_processor_to_map(UINT32 acpi_id, ACPI_HANDLE obj) 1319 { 1320 int cpu_id; 1321 1322 /* 1323 * Special case: if we're a uppc system, there won't be 1324 * a CPU map yet. So we create one and use the passed-in 1325 * processor as CPU 0 1326 */ 1327 if (cpu_map == NULL) { 1328 cpu_map = kmem_zalloc(sizeof (cpu_map[0]) * NCPU, KM_SLEEP); 1329 cpu_map[0] = kmem_zalloc(sizeof (*cpu_map[0]), KM_SLEEP); 1330 cpu_map[0]->obj = obj; 1331 cpu_map_count = 1; 1332 return; 1333 } 1334 1335 for (cpu_id = 0; cpu_id < NCPU; cpu_id++) { 1336 if (cpu_map[cpu_id] == NULL) 1337 continue; 1338 1339 if (cpu_map[cpu_id]->mpa->ProcessorId == acpi_id) { 1340 cpu_map[cpu_id]->obj = obj; 1341 break; 1342 } 1343 } 1344 1345 } 1346 1347 /* 1348 * 1349 */ 1350 ACPI_STATUS 1351 acpica_get_handle_cpu(dev_info_t *dip, ACPI_HANDLE *rh) 1352 { 1353 char *device_type_prop; 1354 int cpu_id; 1355 1356 /* 1357 * if "device_type" != "cpu", error 1358 */ 1359 if ((ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0, 1360 "device_type", &device_type_prop) != DDI_PROP_SUCCESS) || 1361 (strcmp("cpu", device_type_prop) != 0)) { 1362 ddi_prop_free(device_type_prop); 1363 return (AE_ERROR); 1364 } 1365 ddi_prop_free(device_type_prop); 1366 1367 /* 1368 * if cpu_map itself is NULL, we're a uppc system and 1369 * acpica_build_processor_map() hasn't been called yet. 1370 * So call it here 1371 */ 1372 if (cpu_map == NULL) { 1373 (void) acpica_build_processor_map(); 1374 if (cpu_map == NULL) 1375 return (AE_ERROR); 1376 } 1377 1378 /* 1379 * get 'reg' and get obj from cpu_map 1380 */ 1381 cpu_id = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 1382 "reg", -1); 1383 if ((cpu_id < 0) || (cpu_map[cpu_id] == NULL) || 1384 (cpu_map[cpu_id]->obj == NULL)) 1385 return (AE_ERROR); 1386 1387 /* 1388 * tag devinfo and obj 1389 */ 1390 (void) acpica_tag_devinfo(dip, cpu_map[cpu_id]->obj); 1391 *rh = cpu_map[cpu_id]->obj; 1392 return (AE_OK); 1393 } 1394 1395 /* 1396 * Determine if this object is a processor 1397 */ 1398 static ACPI_STATUS 1399 acpica_probe_processor(ACPI_HANDLE obj, UINT32 level, void *ctx, void **rv) 1400 { 1401 ACPI_STATUS status; 1402 ACPI_OBJECT_TYPE objtype; 1403 UINT32 acpi_id; 1404 ACPI_BUFFER rb; 1405 1406 if (AcpiGetType(obj, &objtype) != AE_OK) 1407 return (AE_OK); 1408 1409 if (objtype == ACPI_TYPE_PROCESSOR) { 1410 /* process a Processor */ 1411 rb.Pointer = NULL; 1412 rb.Length = ACPI_ALLOCATE_BUFFER; 1413 status = AcpiEvaluateObject(obj, NULL, NULL, &rb); 1414 if (status != AE_OK) { 1415 cmn_err(CE_WARN, "acpica: error probing Processor"); 1416 return (status); 1417 } 1418 ASSERT(((ACPI_OBJECT *)rb.Pointer)->Type == 1419 ACPI_TYPE_PROCESSOR); 1420 acpi_id = ((ACPI_OBJECT *)rb.Pointer)->Processor.ProcId; 1421 AcpiOsFree(rb.Pointer); 1422 } else if (objtype == ACPI_TYPE_DEVICE) { 1423 /* process a processor Device */ 1424 cmn_err(CE_WARN, "!acpica: probe found a processor Device\n"); 1425 cmn_err(CE_WARN, "!acpica: no support for processor Devices\n"); 1426 return (AE_OK); 1427 } 1428 1429 acpica_add_processor_to_map(acpi_id, obj); 1430 return (AE_OK); 1431 } 1432 1433 1434 static void 1435 scan_d2a_map(void) 1436 { 1437 dev_info_t *dip, *cdip; 1438 ACPI_HANDLE acpiobj; 1439 char *device_type_prop; 1440 int bus; 1441 static int map_error = 0; 1442 1443 if (map_error) 1444 return; 1445 1446 scanning_d2a_map = 1; 1447 1448 /* 1449 * Find all child-of-root PCI buses, and find their corresponding 1450 * ACPI child-of-root PCI nodes. For each one, add to the 1451 * d2a table. 1452 */ 1453 1454 for (dip = ddi_get_child(ddi_root_node()); 1455 dip != NULL; 1456 dip = ddi_get_next_sibling(dip)) { 1457 1458 /* prune non-PCI nodes */ 1459 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0, 1460 "device_type", &device_type_prop) != DDI_PROP_SUCCESS) 1461 continue; 1462 1463 if ((strcmp("pci", device_type_prop) != 0) && 1464 (strcmp("pciex", device_type_prop) != 0)) { 1465 ddi_prop_free(device_type_prop); 1466 continue; 1467 } 1468 1469 ddi_prop_free(device_type_prop); 1470 1471 /* 1472 * To get bus number of dip, get first child and get its 1473 * bus number. If NULL, just continue, because we don't 1474 * care about bus nodes with no children anyway. 1475 */ 1476 if ((cdip = ddi_get_child(dip)) == NULL) 1477 continue; 1478 1479 if (acpica_get_bdf(cdip, &bus, NULL, NULL) < 0) { 1480 #ifdef D2ADEBUG 1481 cmn_err(CE_WARN, "Can't get bus number of PCI child?"); 1482 #endif 1483 map_error = 1; 1484 scanning_d2a_map = 0; 1485 d2a_done = 1; 1486 return; 1487 } 1488 1489 if (acpica_find_pcibus(bus, &acpiobj) == AE_ERROR) { 1490 #ifdef D2ADEBUG 1491 cmn_err(CE_WARN, "No ACPI bus obj for bus %d?\n", bus); 1492 #endif 1493 map_error = 1; 1494 continue; 1495 } 1496 1497 acpica_tag_devinfo(dip, acpiobj); 1498 1499 /* call recursively to enumerate subtrees */ 1500 scan_d2a_subtree(dip, acpiobj, bus); 1501 } 1502 1503 scanning_d2a_map = 0; 1504 d2a_done = 1; 1505 } 1506 1507 /* 1508 * For all acpi child devices of acpiobj, find their matching 1509 * dip under "dip" argument. (matching means "matches dev/fn"). 1510 * bus is assumed to already be a match from caller, and is 1511 * used here only to record in the d2a entry. Recurse if necessary. 1512 */ 1513 static void 1514 scan_d2a_subtree(dev_info_t *dip, ACPI_HANDLE acpiobj, int bus) 1515 { 1516 int acpi_devfn, hid; 1517 ACPI_HANDLE acld; 1518 dev_info_t *dcld; 1519 int dcld_b, dcld_d, dcld_f; 1520 int dev, func; 1521 char *device_type_prop; 1522 1523 acld = NULL; 1524 while (AcpiGetNextObject(ACPI_TYPE_DEVICE, acpiobj, acld, &acld) 1525 == AE_OK) { 1526 /* get the dev/func we're looking for in the devinfo tree */ 1527 if (acpica_eval_int(acld, "_ADR", &acpi_devfn) != AE_OK) 1528 continue; 1529 dev = (acpi_devfn >> 16) & 0xFFFF; 1530 func = acpi_devfn & 0xFFFF; 1531 1532 /* look through all the immediate children of dip */ 1533 for (dcld = ddi_get_child(dip); dcld != NULL; 1534 dcld = ddi_get_next_sibling(dcld)) { 1535 if (acpica_get_bdf(dcld, &dcld_b, &dcld_d, &dcld_f) < 0) 1536 continue; 1537 1538 /* dev must match; function must match or wildcard */ 1539 if (dcld_d != dev || 1540 (func != 0xFFFF && func != dcld_f)) 1541 continue; 1542 bus = dcld_b; 1543 1544 /* found a match, record it */ 1545 acpica_tag_devinfo(dcld, acld); 1546 1547 /* if we find a bridge, recurse from here */ 1548 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dcld, 0, 1549 "device_type", &device_type_prop) == 1550 DDI_PROP_SUCCESS) { 1551 if ((strcmp("pci", device_type_prop) == 0) || 1552 (strcmp("pciex", device_type_prop) == 0)) 1553 scan_d2a_subtree(dcld, acld, bus); 1554 ddi_prop_free(device_type_prop); 1555 } 1556 1557 /* done finding a match, so break now */ 1558 break; 1559 } 1560 } 1561 } 1562 1563 /* 1564 * Return bus/dev/fn for PCI dip (note: not the parent "pci" node). 1565 */ 1566 int 1567 acpica_get_bdf(dev_info_t *dip, int *bus, int *device, int *func) 1568 { 1569 pci_regspec_t *pci_rp; 1570 int len; 1571 1572 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 1573 "reg", (int **)&pci_rp, (uint_t *)&len) != DDI_SUCCESS) 1574 return (-1); 1575 1576 if (len < (sizeof (pci_regspec_t) / sizeof (int))) { 1577 ddi_prop_free(pci_rp); 1578 return (-1); 1579 } 1580 if (bus != NULL) 1581 *bus = (int)PCI_REG_BUS_G(pci_rp->pci_phys_hi); 1582 if (device != NULL) 1583 *device = (int)PCI_REG_DEV_G(pci_rp->pci_phys_hi); 1584 if (func != NULL) 1585 *func = (int)PCI_REG_FUNC_G(pci_rp->pci_phys_hi); 1586 ddi_prop_free(pci_rp); 1587 return (0); 1588 } 1589 1590 /* 1591 * Return the ACPI device node matching this dev_info node, if it 1592 * exists in the ACPI tree. 1593 */ 1594 ACPI_STATUS 1595 acpica_get_handle(dev_info_t *dip, ACPI_HANDLE *rh) 1596 { 1597 ACPI_STATUS status; 1598 char *acpiname; 1599 1600 if (!d2a_done) 1601 scan_d2a_map(); 1602 1603 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 1604 "acpi-namespace", &acpiname) != DDI_PROP_SUCCESS) { 1605 return (acpica_get_handle_cpu(dip, rh)); 1606 } 1607 1608 status = AcpiGetHandle(NULL, acpiname, rh); 1609 ddi_prop_free((void *)acpiname); 1610 return (status); 1611 } 1612 1613 1614 1615 /* 1616 * Manage OS data attachment to ACPI nodes 1617 */ 1618 1619 /* 1620 * Return the (dev_info_t *) associated with the ACPI node. 1621 */ 1622 ACPI_STATUS 1623 acpica_get_devinfo(ACPI_HANDLE obj, dev_info_t **dipp) 1624 { 1625 ACPI_STATUS status; 1626 void *ptr; 1627 1628 status = AcpiGetData(obj, acpica_devinfo_handler, &ptr); 1629 if (status == AE_OK) 1630 *dipp = (dev_info_t *)ptr; 1631 1632 return (status); 1633 } 1634 1635 /* 1636 * Set the dev_info_t associated with the ACPI node. 1637 */ 1638 static ACPI_STATUS 1639 acpica_set_devinfo(ACPI_HANDLE obj, dev_info_t *dip) 1640 { 1641 ACPI_STATUS status; 1642 1643 status = AcpiAttachData(obj, acpica_devinfo_handler, (void *)dip); 1644 return (status); 1645 } 1646 1647 1648 /* 1649 * 1650 */ 1651 void 1652 acpica_devinfo_handler(ACPI_HANDLE obj, UINT32 func, void *data) 1653 { 1654 /* noop */ 1655 } 1656 1657 1658 /* 1659 * 1660 */ 1661 void 1662 acpica_map_cpu(processorid_t cpuid, MADT_PROCESSOR_APIC *mpa) 1663 { 1664 struct cpu_map_item *item; 1665 1666 if (cpu_map == NULL) 1667 cpu_map = kmem_zalloc(sizeof (item) * NCPU, KM_SLEEP); 1668 1669 item = kmem_zalloc(sizeof (*item), KM_SLEEP); 1670 item->mpa = mpa; 1671 item->obj = NULL; 1672 cpu_map[cpuid] = item; 1673 cpu_map_count++; 1674 } 1675 1676 void 1677 acpica_build_processor_map() 1678 { 1679 ACPI_STATUS status; 1680 void *rv; 1681 1682 /* 1683 * shouldn't be called more than once anyway 1684 */ 1685 if (cpu_map_built) 1686 return; 1687 1688 /* 1689 * Look for Processor objects 1690 */ 1691 status = AcpiWalkNamespace(ACPI_TYPE_PROCESSOR, 1692 ACPI_ROOT_OBJECT, 1693 4, 1694 acpica_probe_processor, 1695 NULL, 1696 &rv); 1697 ASSERT(status == AE_OK); 1698 1699 /* 1700 * Look for processor Device objects 1701 */ 1702 status = AcpiGetDevices("ACPI0007", 1703 acpica_probe_processor, 1704 NULL, 1705 &rv); 1706 ASSERT(status == AE_OK); 1707 cpu_map_built = 1; 1708 } 1709