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