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