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