xref: /illumos-gate/usr/src/uts/intel/io/acpica/osl.c (revision 78d5422c)
17c478bd9Sstevel@tonic-gate /*
28e56767dSsmall  * CDDL HEADER START
38e56767dSsmall  *
48e56767dSsmall  * The contents of this file are subject to the terms of the
530082d0cSmyers  * Common Development and Distribution License (the "License").
630082d0cSmyers  * You may not use this file except in compliance with the License.
78e56767dSsmall  *
88e56767dSsmall  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
98e56767dSsmall  * or http://www.opensolaris.org/os/licensing.
108e56767dSsmall  * See the License for the specific language governing permissions
118e56767dSsmall  * and limitations under the License.
128e56767dSsmall  *
138e56767dSsmall  * When distributing Covered Code, include this CDDL HEADER in each
148e56767dSsmall  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
158e56767dSsmall  * If applicable, add the following below this CDDL HEADER, with the
168e56767dSsmall  * fields enclosed by brackets "[]" replaced with your own identifying
178e56767dSsmall  * information: Portions Copyright [yyyy] [name of copyright owner]
188e56767dSsmall  *
198e56767dSsmall  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21450d6964Smyers 
227c478bd9Sstevel@tonic-gate /*
230e751525SEric Saxe  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
26fa96bd91SMichael Corcoran /*
27fa96bd91SMichael Corcoran  * Copyright (c) 2009, Intel Corporation.
28fa96bd91SMichael Corcoran  * All rights reserved.
29fa96bd91SMichael Corcoran  */
308e56767dSsmall /*
318e56767dSsmall  * ACPI CA OSL for Solaris x86
328e56767dSsmall  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
367c478bd9Sstevel@tonic-gate #include <sys/psm.h>
377c478bd9Sstevel@tonic-gate #include <sys/pci_cfgspace.h>
38b9bfdccdSStuart Maybee #include <sys/apic.h>
397c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
404b1ce0a9SDana Myers #include <sys/sunddi.h>
415cff7825Smh #include <sys/sunndi.h>
427c478bd9Sstevel@tonic-gate #include <sys/pci.h>
437c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
447c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
457c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
467c478bd9Sstevel@tonic-gate #include <sys/note.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <sys/acpi/acpi.h>
49*78d5422cSMark Haywood #include <sys/acpi/accommon.h>
507c478bd9Sstevel@tonic-gate #include <sys/acpica.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	MAX_DAT_FILE_SIZE	(64*1024)
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /* local functions */
557c478bd9Sstevel@tonic-gate static int CompressEisaID(char *np);
567c478bd9Sstevel@tonic-gate 
575cff7825Smh static void scan_d2a_map(void);
585cff7825Smh static void scan_d2a_subtree(dev_info_t *dip, ACPI_HANDLE acpiobj, int bus);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static int acpica_query_bbn_problem(void);
617c478bd9Sstevel@tonic-gate static int acpica_find_pcibus(int busno, ACPI_HANDLE *rh);
627c478bd9Sstevel@tonic-gate static int acpica_eval_hid(ACPI_HANDLE dev, char *method, int *rint);
635cff7825Smh static ACPI_STATUS acpica_set_devinfo(ACPI_HANDLE, dev_info_t *);
64fa96bd91SMichael Corcoran static ACPI_STATUS acpica_unset_devinfo(ACPI_HANDLE);
655cff7825Smh static void acpica_devinfo_handler(ACPI_HANDLE, UINT32, void *);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Event queue vars
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate int acpica_eventq_init = 0;
7127f7c583Smyers ddi_taskq_t *osl_eventq[OSL_EC_BURST_HANDLER+1];
727c478bd9Sstevel@tonic-gate 
73db2bae30SDana Myers /*
74db2bae30SDana Myers  * Priorities relative to minclsyspri that each taskq
75db2bae30SDana Myers  * run at; OSL_NOTIFY_HANDLER needs to run at a higher
76db2bae30SDana Myers  * priority than OSL_GPE_HANDLER.  There's an implicit
77db2bae30SDana Myers  * assumption that no priority here results in exceeding
78db2bae30SDana Myers  * maxclsyspri.
79db2bae30SDana Myers  * Note: these initializations need to match the order of
80db2bae30SDana Myers  * ACPI_EXECUTE_TYPE.
81db2bae30SDana Myers  */
82db2bae30SDana Myers int osl_eventq_pri_delta[OSL_EC_BURST_HANDLER+1] = {
83db2bae30SDana Myers 	0,	/* OSL_GLOBAL_LOCK_HANDLER */
84db2bae30SDana Myers 	2,	/* OSL_NOTIFY_HANDLER */
85db2bae30SDana Myers 	0,	/* OSL_GPE_HANDLER */
86db2bae30SDana Myers 	0,	/* OSL_DEBUGGER_THREAD */
87db2bae30SDana Myers 	0,	/* OSL_EC_POLL_HANDLER */
88db2bae30SDana Myers 	0	/* OSL_EC_BURST_HANDLER */
89db2bae30SDana Myers };
90db2bae30SDana Myers 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * Note, if you change this path, you need to update
937c478bd9Sstevel@tonic-gate  * /boot/grub/filelist.ramdisk and pkg SUNWckr/prototype_i386
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate static char *acpi_table_path = "/boot/acpi/tables/";
967c478bd9Sstevel@tonic-gate 
975cff7825Smh /* non-zero while scan_d2a_map() is working */
985cff7825Smh static int scanning_d2a_map = 0;
995cff7825Smh static int d2a_done = 0;
1007c478bd9Sstevel@tonic-gate 
101fa96bd91SMichael Corcoran /* features supported by ACPICA and ACPI device configuration. */
102fa96bd91SMichael Corcoran uint64_t acpica_core_features = 0;
103fa96bd91SMichael Corcoran static uint64_t acpica_devcfg_features = 0;
104fa96bd91SMichael Corcoran 
105137e2f25SGuoli Shu /* set by acpi_poweroff() in PSMs and appm_ioctl() in acpippm for S3 */
106137e2f25SGuoli Shu int acpica_use_safe_delay = 0;
107859dcd3dSmyers 
1085cff7825Smh /* CPU mapping data */
1095cff7825Smh struct cpu_map_item {
110fa96bd91SMichael Corcoran 	processorid_t	cpu_id;
111b6917abeSmishra 	UINT32		proc_id;
112fa96bd91SMichael Corcoran 	UINT32		apic_id;
1135cff7825Smh 	ACPI_HANDLE	obj;
1145cff7825Smh };
1155cff7825Smh 
116fa96bd91SMichael Corcoran static kmutex_t cpu_map_lock;
1175cff7825Smh static struct cpu_map_item **cpu_map = NULL;
118fa96bd91SMichael Corcoran static int cpu_map_count_max = 0;
1195cff7825Smh static int cpu_map_count = 0;
1205cff7825Smh static int cpu_map_built = 0;
1215cff7825Smh 
1225cff7825Smh static int acpi_has_broken_bbn = -1;
1235cff7825Smh 
12487bb58d6Smyers /* buffer for AcpiOsVprintf() */
12587bb58d6Smyers #define	ACPI_OSL_PR_BUFLEN	1024
12687bb58d6Smyers static char *acpi_osl_pr_buffer = NULL;
12787bb58d6Smyers static int acpi_osl_pr_buflen;
12887bb58d6Smyers 
1295cff7825Smh #define	D2A_DEBUG
1305cff7825Smh 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  *
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate static void
1357c478bd9Sstevel@tonic-gate discard_event_queues()
1367c478bd9Sstevel@tonic-gate {
13727f7c583Smyers 	int	i;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/*
1407c478bd9Sstevel@tonic-gate 	 * destroy event queues
1417c478bd9Sstevel@tonic-gate 	 */
14227f7c583Smyers 	for (i = OSL_GLOBAL_LOCK_HANDLER; i <= OSL_EC_BURST_HANDLER; i++) {
14327f7c583Smyers 		if (osl_eventq[i])
14427f7c583Smyers 			ddi_taskq_destroy(osl_eventq[i]);
14527f7c583Smyers 	}
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate  *
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate static ACPI_STATUS
1537c478bd9Sstevel@tonic-gate init_event_queues()
1547c478bd9Sstevel@tonic-gate {
15527f7c583Smyers 	char	namebuf[32];
15627f7c583Smyers 	int	i, error = 0;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/*
1597c478bd9Sstevel@tonic-gate 	 * Initialize event queues
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 
162db2bae30SDana Myers 	/* Always allocate only 1 thread per queue to force FIFO execution */
16327f7c583Smyers 	for (i = OSL_GLOBAL_LOCK_HANDLER; i <= OSL_EC_BURST_HANDLER; i++) {
16427f7c583Smyers 		snprintf(namebuf, 32, "ACPI%d", i);
165db2bae30SDana Myers 		osl_eventq[i] = ddi_taskq_create(NULL, namebuf, 1,
166db2bae30SDana Myers 		    osl_eventq_pri_delta[i] + minclsyspri, 0);
16727f7c583Smyers 		if (osl_eventq[i] == NULL)
16827f7c583Smyers 			error++;
16927f7c583Smyers 	}
17027f7c583Smyers 
17127f7c583Smyers 	if (error != 0) {
1727c478bd9Sstevel@tonic-gate 		discard_event_queues();
1737c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1747c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!acpica: could not initialize event queues");
1757c478bd9Sstevel@tonic-gate #endif
1767c478bd9Sstevel@tonic-gate 		return (AE_ERROR);
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	acpica_eventq_init = 1;
1807c478bd9Sstevel@tonic-gate 	return (AE_OK);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
18487bb58d6Smyers  * One-time initialization of OSL layer
1857c478bd9Sstevel@tonic-gate  */
1867c478bd9Sstevel@tonic-gate ACPI_STATUS
1877c478bd9Sstevel@tonic-gate AcpiOsInitialize(void)
1887c478bd9Sstevel@tonic-gate {
18987bb58d6Smyers 	/*
19087bb58d6Smyers 	 * Allocate buffer for AcpiOsVprintf() here to avoid
19187bb58d6Smyers 	 * kmem_alloc()/kmem_free() at high PIL
19287bb58d6Smyers 	 */
19387bb58d6Smyers 	acpi_osl_pr_buffer = kmem_alloc(ACPI_OSL_PR_BUFLEN, KM_SLEEP);
19487bb58d6Smyers 	if (acpi_osl_pr_buffer != NULL)
19587bb58d6Smyers 		acpi_osl_pr_buflen = ACPI_OSL_PR_BUFLEN;
19687bb58d6Smyers 
1977c478bd9Sstevel@tonic-gate 	return (AE_OK);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /*
20187bb58d6Smyers  * One-time shut-down of OSL layer
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate ACPI_STATUS
2047c478bd9Sstevel@tonic-gate AcpiOsTerminate(void)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 
20787bb58d6Smyers 	if (acpi_osl_pr_buffer != NULL)
20887bb58d6Smyers 		kmem_free(acpi_osl_pr_buffer, acpi_osl_pr_buflen);
20987bb58d6Smyers 
2107c478bd9Sstevel@tonic-gate 	discard_event_queues();
2117c478bd9Sstevel@tonic-gate 	return (AE_OK);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 
215db2bae30SDana Myers ACPI_PHYSICAL_ADDRESS
216db2bae30SDana Myers AcpiOsGetRootPointer()
2177c478bd9Sstevel@tonic-gate {
218db2bae30SDana Myers 	ACPI_PHYSICAL_ADDRESS Address;
219f2be5148Sszhou 
220f2be5148Sszhou 	/*
221f2be5148Sszhou 	 * For EFI firmware, the root pointer is defined in EFI systab.
222f2be5148Sszhou 	 * The boot code process the table and put the physical address
223f2be5148Sszhou 	 * in the acpi-root-tab property.
224f2be5148Sszhou 	 */
225c1381f44SDana Myers 	Address = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_root_node(),
226c1381f44SDana Myers 	    DDI_PROP_DONTPASS, "acpi-root-tab", NULL);
227db2bae30SDana Myers 
228db2bae30SDana Myers 	if ((Address == NULL) && ACPI_FAILURE(AcpiFindRootPointer(&Address)))
229db2bae30SDana Myers 		Address = NULL;
230db2bae30SDana Myers 
231db2bae30SDana Myers 	return (Address);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2357c478bd9Sstevel@tonic-gate ACPI_STATUS
2367c478bd9Sstevel@tonic-gate AcpiOsPredefinedOverride(const ACPI_PREDEFINED_NAMES *InitVal,
2377c478bd9Sstevel@tonic-gate 				ACPI_STRING *NewVal)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	*NewVal = 0;
2417c478bd9Sstevel@tonic-gate 	return (AE_OK);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate static void
2457c478bd9Sstevel@tonic-gate acpica_strncpy(char *dest, const char *src, int len)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	/*LINTED*/
2497c478bd9Sstevel@tonic-gate 	while ((*dest++ = *src++) && (--len > 0))
2507c478bd9Sstevel@tonic-gate 		/* copy the string */;
2517c478bd9Sstevel@tonic-gate 	*dest = '\0';
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate ACPI_STATUS
2557c478bd9Sstevel@tonic-gate AcpiOsTableOverride(ACPI_TABLE_HEADER *ExistingTable,
2567c478bd9Sstevel@tonic-gate 			ACPI_TABLE_HEADER **NewTable)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	char signature[5];
2597c478bd9Sstevel@tonic-gate 	char oemid[7];
2607c478bd9Sstevel@tonic-gate 	char oemtableid[9];
2617c478bd9Sstevel@tonic-gate 	struct _buf *file;
2627c478bd9Sstevel@tonic-gate 	char *buf1, *buf2;
2637c478bd9Sstevel@tonic-gate 	int count;
2647c478bd9Sstevel@tonic-gate 	char acpi_table_loc[128];
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	acpica_strncpy(signature, ExistingTable->Signature, 4);
2677c478bd9Sstevel@tonic-gate 	acpica_strncpy(oemid, ExistingTable->OemId, 6);
2687c478bd9Sstevel@tonic-gate 	acpica_strncpy(oemtableid, ExistingTable->OemTableId, 8);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate #ifdef	DEBUG
2717c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!acpica: table [%s] v%d OEM ID [%s]"
2725dff2f34Smyers 	    " OEM TABLE ID [%s] OEM rev %x",
2735dff2f34Smyers 	    signature, ExistingTable->Revision, oemid, oemtableid,
2745dff2f34Smyers 	    ExistingTable->OemRevision);
2757c478bd9Sstevel@tonic-gate #endif
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	/* File name format is "signature_oemid_oemtableid.dat" */
2787c478bd9Sstevel@tonic-gate 	(void) strcpy(acpi_table_loc, acpi_table_path);
2797c478bd9Sstevel@tonic-gate 	(void) strcat(acpi_table_loc, signature); /* for example, DSDT */
2807c478bd9Sstevel@tonic-gate 	(void) strcat(acpi_table_loc, "_");
2817c478bd9Sstevel@tonic-gate 	(void) strcat(acpi_table_loc, oemid); /* for example, IntelR */
2827c478bd9Sstevel@tonic-gate 	(void) strcat(acpi_table_loc, "_");
2837c478bd9Sstevel@tonic-gate 	(void) strcat(acpi_table_loc, oemtableid); /* for example, AWRDACPI */
2847c478bd9Sstevel@tonic-gate 	(void) strcat(acpi_table_loc, ".dat");
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	file = kobj_open_file(acpi_table_loc);
2877c478bd9Sstevel@tonic-gate 	if (file == (struct _buf *)-1) {
2887c478bd9Sstevel@tonic-gate 		*NewTable = 0;
2897c478bd9Sstevel@tonic-gate 		return (AE_OK);
2907c478bd9Sstevel@tonic-gate 	} else {
2917c478bd9Sstevel@tonic-gate 		buf1 = (char *)kmem_alloc(MAX_DAT_FILE_SIZE, KM_SLEEP);
2927c478bd9Sstevel@tonic-gate 		count = kobj_read_file(file, buf1, MAX_DAT_FILE_SIZE-1, 0);
2937c478bd9Sstevel@tonic-gate 		if (count >= MAX_DAT_FILE_SIZE) {
2947c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "!acpica: table %s file size too big",
2957c478bd9Sstevel@tonic-gate 			    acpi_table_loc);
2967c478bd9Sstevel@tonic-gate 			*NewTable = 0;
2977c478bd9Sstevel@tonic-gate 		} else {
2987c478bd9Sstevel@tonic-gate 			buf2 = (char *)kmem_alloc(count, KM_SLEEP);
2997c478bd9Sstevel@tonic-gate 			(void) memcpy(buf2, buf1, count);
3007c478bd9Sstevel@tonic-gate 			*NewTable = (ACPI_TABLE_HEADER *)buf2;
3017c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "!acpica: replacing table: %s",
3027c478bd9Sstevel@tonic-gate 			    acpi_table_loc);
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 	kobj_close_file(file);
3067c478bd9Sstevel@tonic-gate 	kmem_free(buf1, MAX_DAT_FILE_SIZE);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	return (AE_OK);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 
31230082d0cSmyers /*
31330082d0cSmyers  * ACPI semaphore implementation
31430082d0cSmyers  */
31530082d0cSmyers typedef struct {
31630082d0cSmyers 	kmutex_t	mutex;
31730082d0cSmyers 	kcondvar_t	cv;
31830082d0cSmyers 	uint32_t	available;
31930082d0cSmyers 	uint32_t	initial;
32030082d0cSmyers 	uint32_t	maximum;
32130082d0cSmyers } acpi_sema_t;
32230082d0cSmyers 
32330082d0cSmyers /*
32430082d0cSmyers  *
32530082d0cSmyers  */
32630082d0cSmyers void
32727f7c583Smyers acpi_sema_init(acpi_sema_t *sp, unsigned max, unsigned count)
32830082d0cSmyers {
32930082d0cSmyers 	mutex_init(&sp->mutex, NULL, MUTEX_DRIVER, NULL);
33030082d0cSmyers 	cv_init(&sp->cv, NULL, CV_DRIVER, NULL);
33130082d0cSmyers 	/* no need to enter mutex here at creation */
33230082d0cSmyers 	sp->available = count;
33330082d0cSmyers 	sp->initial = count;
33430082d0cSmyers 	sp->maximum = max;
33530082d0cSmyers }
33630082d0cSmyers 
33730082d0cSmyers /*
33830082d0cSmyers  *
33930082d0cSmyers  */
34030082d0cSmyers void
34130082d0cSmyers acpi_sema_destroy(acpi_sema_t *sp)
34230082d0cSmyers {
34330082d0cSmyers 
34430082d0cSmyers 	cv_destroy(&sp->cv);
34530082d0cSmyers 	mutex_destroy(&sp->mutex);
34630082d0cSmyers }
34730082d0cSmyers 
34830082d0cSmyers /*
34930082d0cSmyers  *
35030082d0cSmyers  */
35130082d0cSmyers ACPI_STATUS
35230082d0cSmyers acpi_sema_p(acpi_sema_t *sp, unsigned count, uint16_t wait_time)
35330082d0cSmyers {
35430082d0cSmyers 	ACPI_STATUS rv = AE_OK;
35530082d0cSmyers 	clock_t deadline;
35630082d0cSmyers 
35730082d0cSmyers 	mutex_enter(&sp->mutex);
35830082d0cSmyers 
35930082d0cSmyers 	if (sp->available >= count) {
36030082d0cSmyers 		/*
36130082d0cSmyers 		 * Enough units available, no blocking
36230082d0cSmyers 		 */
36330082d0cSmyers 		sp->available -= count;
36430082d0cSmyers 		mutex_exit(&sp->mutex);
36530082d0cSmyers 		return (rv);
36630082d0cSmyers 	} else if (wait_time == 0) {
36730082d0cSmyers 		/*
36830082d0cSmyers 		 * Not enough units available and timeout
36930082d0cSmyers 		 * specifies no blocking
37030082d0cSmyers 		 */
37130082d0cSmyers 		rv = AE_TIME;
37230082d0cSmyers 		mutex_exit(&sp->mutex);
37330082d0cSmyers 		return (rv);
37430082d0cSmyers 	}
37530082d0cSmyers 
37630082d0cSmyers 	/*
37730082d0cSmyers 	 * Not enough units available and timeout specifies waiting
37830082d0cSmyers 	 */
37930082d0cSmyers 	if (wait_time != ACPI_WAIT_FOREVER)
38030082d0cSmyers 		deadline = ddi_get_lbolt() +
38130082d0cSmyers 		    (clock_t)drv_usectohz(wait_time * 1000);
38230082d0cSmyers 
38330082d0cSmyers 	do {
38430082d0cSmyers 		if (wait_time == ACPI_WAIT_FOREVER)
38530082d0cSmyers 			cv_wait(&sp->cv, &sp->mutex);
38630082d0cSmyers 		else if (cv_timedwait(&sp->cv, &sp->mutex, deadline) < 0) {
38730082d0cSmyers 			rv = AE_TIME;
38830082d0cSmyers 			break;
38930082d0cSmyers 		}
39030082d0cSmyers 	} while (sp->available < count);
39130082d0cSmyers 
39230082d0cSmyers 	/* if we dropped out of the wait with AE_OK, we got the units */
39330082d0cSmyers 	if (rv == AE_OK)
39430082d0cSmyers 		sp->available -= count;
39530082d0cSmyers 
39630082d0cSmyers 	mutex_exit(&sp->mutex);
39730082d0cSmyers 	return (rv);
39830082d0cSmyers }
39930082d0cSmyers 
40030082d0cSmyers /*
40130082d0cSmyers  *
40230082d0cSmyers  */
40330082d0cSmyers void
40430082d0cSmyers acpi_sema_v(acpi_sema_t *sp, unsigned count)
40530082d0cSmyers {
40630082d0cSmyers 	mutex_enter(&sp->mutex);
40730082d0cSmyers 	sp->available += count;
40830082d0cSmyers 	cv_broadcast(&sp->cv);
40930082d0cSmyers 	mutex_exit(&sp->mutex);
41030082d0cSmyers }
41130082d0cSmyers 
41230082d0cSmyers 
4137c478bd9Sstevel@tonic-gate ACPI_STATUS
4147c478bd9Sstevel@tonic-gate AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits,
4157c478bd9Sstevel@tonic-gate ACPI_HANDLE *OutHandle)
4167c478bd9Sstevel@tonic-gate {
41730082d0cSmyers 	acpi_sema_t *sp;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	if ((OutHandle == NULL) || (InitialUnits > MaxUnits))
4207c478bd9Sstevel@tonic-gate 		return (AE_BAD_PARAMETER);
4217c478bd9Sstevel@tonic-gate 
42230082d0cSmyers 	sp = (acpi_sema_t *)kmem_alloc(sizeof (acpi_sema_t), KM_SLEEP);
42327f7c583Smyers 	acpi_sema_init(sp, MaxUnits, InitialUnits);
42430082d0cSmyers 	*OutHandle = (ACPI_HANDLE)sp;
4257c478bd9Sstevel@tonic-gate 	return (AE_OK);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate ACPI_STATUS
4307c478bd9Sstevel@tonic-gate AcpiOsDeleteSemaphore(ACPI_HANDLE Handle)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate 
43330082d0cSmyers 	if (Handle == NULL)
43430082d0cSmyers 		return (AE_BAD_PARAMETER);
43530082d0cSmyers 
43630082d0cSmyers 	acpi_sema_destroy((acpi_sema_t *)Handle);
43730082d0cSmyers 	kmem_free((void *)Handle, sizeof (acpi_sema_t));
4387c478bd9Sstevel@tonic-gate 	return (AE_OK);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate ACPI_STATUS
4427c478bd9Sstevel@tonic-gate AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT16 Timeout)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 
44530082d0cSmyers 	if ((Handle == NULL) || (Units < 1))
44630082d0cSmyers 		return (AE_BAD_PARAMETER);
4477c478bd9Sstevel@tonic-gate 
44830082d0cSmyers 	return (acpi_sema_p((acpi_sema_t *)Handle, Units, Timeout));
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate ACPI_STATUS
4527c478bd9Sstevel@tonic-gate AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units)
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate 
45530082d0cSmyers 	if ((Handle == NULL) || (Units < 1))
45630082d0cSmyers 		return (AE_BAD_PARAMETER);
45730082d0cSmyers 
45830082d0cSmyers 	acpi_sema_v((acpi_sema_t *)Handle, Units);
4597c478bd9Sstevel@tonic-gate 	return (AE_OK);
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate ACPI_STATUS
4637c478bd9Sstevel@tonic-gate AcpiOsCreateLock(ACPI_HANDLE *OutHandle)
4647c478bd9Sstevel@tonic-gate {
46530082d0cSmyers 	kmutex_t *mp;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if (OutHandle == NULL)
4687c478bd9Sstevel@tonic-gate 		return (AE_BAD_PARAMETER);
4697c478bd9Sstevel@tonic-gate 
47030082d0cSmyers 	mp = (kmutex_t *)kmem_alloc(sizeof (kmutex_t), KM_SLEEP);
47130082d0cSmyers 	mutex_init(mp, NULL, MUTEX_DRIVER, NULL);
47230082d0cSmyers 	*OutHandle = (ACPI_HANDLE)mp;
4737c478bd9Sstevel@tonic-gate 	return (AE_OK);
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate void
4777c478bd9Sstevel@tonic-gate AcpiOsDeleteLock(ACPI_HANDLE Handle)
4787c478bd9Sstevel@tonic-gate {
47930082d0cSmyers 
48030082d0cSmyers 	if (Handle == NULL)
48130082d0cSmyers 		return;
48230082d0cSmyers 
48330082d0cSmyers 	mutex_destroy((kmutex_t *)Handle);
48430082d0cSmyers 	kmem_free((void *)Handle, sizeof (kmutex_t));
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
487db2bae30SDana Myers ACPI_CPU_FLAGS
488450d6964Smyers AcpiOsAcquireLock(ACPI_HANDLE Handle)
4897c478bd9Sstevel@tonic-gate {
49030082d0cSmyers 
4910e751525SEric Saxe 
4920e751525SEric Saxe 	if (Handle == NULL)
4930e751525SEric Saxe 		return (AE_BAD_PARAMETER);
4940e751525SEric Saxe 
4950e751525SEric Saxe 	if (curthread == CPU->cpu_idle_thread) {
4960e751525SEric Saxe 		while (!mutex_tryenter((kmutex_t *)Handle))
4970e751525SEric Saxe 			/* spin */;
4980e751525SEric Saxe 	} else
4990e751525SEric Saxe 		mutex_enter((kmutex_t *)Handle);
5000e751525SEric Saxe 	return (AE_OK);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate void
504db2bae30SDana Myers AcpiOsReleaseLock(ACPI_HANDLE Handle, ACPI_CPU_FLAGS Flags)
5057c478bd9Sstevel@tonic-gate {
506450d6964Smyers 	_NOTE(ARGUNUSED(Flags))
5077c478bd9Sstevel@tonic-gate 
50830082d0cSmyers 	mutex_exit((kmutex_t *)Handle);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate void *
5137c478bd9Sstevel@tonic-gate AcpiOsAllocate(ACPI_SIZE Size)
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	ACPI_SIZE *tmp_ptr;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	Size += sizeof (Size);
5187c478bd9Sstevel@tonic-gate 	tmp_ptr = (ACPI_SIZE *)kmem_zalloc(Size, KM_SLEEP);
5197c478bd9Sstevel@tonic-gate 	*tmp_ptr++ = Size;
52030082d0cSmyers 	return (tmp_ptr);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate void
5247c478bd9Sstevel@tonic-gate AcpiOsFree(void *Memory)
5257c478bd9Sstevel@tonic-gate {
5267c478bd9Sstevel@tonic-gate 	ACPI_SIZE	size, *tmp_ptr;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	tmp_ptr = (ACPI_SIZE *)Memory;
5297c478bd9Sstevel@tonic-gate 	tmp_ptr -= 1;
5307c478bd9Sstevel@tonic-gate 	size = *tmp_ptr;
53130082d0cSmyers 	kmem_free(tmp_ptr, size);
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate 
534b9bfdccdSStuart Maybee static int napics_found;	/* number of ioapic addresses in array */
535b9bfdccdSStuart Maybee static ACPI_PHYSICAL_ADDRESS ioapic_paddr[MAX_IO_APIC];
536b9bfdccdSStuart Maybee static ACPI_TABLE_MADT *acpi_mapic_dtp = NULL;
537b9bfdccdSStuart Maybee static void *dummy_ioapicadr;
538b9bfdccdSStuart Maybee 
539b9bfdccdSStuart Maybee void
540b9bfdccdSStuart Maybee acpica_find_ioapics(void)
541b9bfdccdSStuart Maybee {
542b9bfdccdSStuart Maybee 	int			madt_seen, madt_size;
543b9bfdccdSStuart Maybee 	ACPI_SUBTABLE_HEADER		*ap;
544b9bfdccdSStuart Maybee 	ACPI_MADT_IO_APIC		*mia;
545b9bfdccdSStuart Maybee 
546b9bfdccdSStuart Maybee 	if (acpi_mapic_dtp != NULL)
547b9bfdccdSStuart Maybee 		return;	/* already parsed table */
548b9bfdccdSStuart Maybee 	if (AcpiGetTable(ACPI_SIG_MADT, 1,
549b9bfdccdSStuart Maybee 	    (ACPI_TABLE_HEADER **) &acpi_mapic_dtp) != AE_OK)
550b9bfdccdSStuart Maybee 		return;
551b9bfdccdSStuart Maybee 
552b9bfdccdSStuart Maybee 	napics_found = 0;
553b9bfdccdSStuart Maybee 
554b9bfdccdSStuart Maybee 	/*
555b9bfdccdSStuart Maybee 	 * Search the MADT for ioapics
556b9bfdccdSStuart Maybee 	 */
557b9bfdccdSStuart Maybee 	ap = (ACPI_SUBTABLE_HEADER *) (acpi_mapic_dtp + 1);
558b9bfdccdSStuart Maybee 	madt_size = acpi_mapic_dtp->Header.Length;
559b9bfdccdSStuart Maybee 	madt_seen = sizeof (*acpi_mapic_dtp);
560b9bfdccdSStuart Maybee 
561b9bfdccdSStuart Maybee 	while (madt_seen < madt_size) {
562b9bfdccdSStuart Maybee 
563b9bfdccdSStuart Maybee 		switch (ap->Type) {
564b9bfdccdSStuart Maybee 		case ACPI_MADT_TYPE_IO_APIC:
565b9bfdccdSStuart Maybee 			mia = (ACPI_MADT_IO_APIC *) ap;
566b9bfdccdSStuart Maybee 			if (napics_found < MAX_IO_APIC) {
567b9bfdccdSStuart Maybee 				ioapic_paddr[napics_found++] =
568b9bfdccdSStuart Maybee 				    (ACPI_PHYSICAL_ADDRESS)
569b9bfdccdSStuart Maybee 				    (mia->Address & PAGEMASK);
570b9bfdccdSStuart Maybee 			}
571b9bfdccdSStuart Maybee 			break;
572b9bfdccdSStuart Maybee 
573b9bfdccdSStuart Maybee 		default:
574b9bfdccdSStuart Maybee 			break;
575b9bfdccdSStuart Maybee 		}
576b9bfdccdSStuart Maybee 
577b9bfdccdSStuart Maybee 		/* advance to next entry */
578b9bfdccdSStuart Maybee 		madt_seen += ap->Length;
579b9bfdccdSStuart Maybee 		ap = (ACPI_SUBTABLE_HEADER *)(((char *)ap) + ap->Length);
580b9bfdccdSStuart Maybee 	}
581b9bfdccdSStuart Maybee 	if (dummy_ioapicadr == NULL)
582b9bfdccdSStuart Maybee 		dummy_ioapicadr = kmem_zalloc(PAGESIZE, KM_SLEEP);
583b9bfdccdSStuart Maybee }
584b9bfdccdSStuart Maybee 
585b9bfdccdSStuart Maybee 
586db2bae30SDana Myers void *
587db2bae30SDana Myers AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Size)
5887c478bd9Sstevel@tonic-gate {
589b9bfdccdSStuart Maybee 	int	i;
590b9bfdccdSStuart Maybee 
591b9bfdccdSStuart Maybee 	/*
592b9bfdccdSStuart Maybee 	 * If the iopaic address table is populated, check if trying
593b9bfdccdSStuart Maybee 	 * to access an ioapic.  Instead, return a pointer to a dummy ioapic.
594b9bfdccdSStuart Maybee 	 */
595b9bfdccdSStuart Maybee 	for (i = 0; i < napics_found; i++) {
596b9bfdccdSStuart Maybee 		if ((PhysicalAddress & PAGEMASK) == ioapic_paddr[i])
597b9bfdccdSStuart Maybee 			return (dummy_ioapicadr);
598b9bfdccdSStuart Maybee 	}
5997c478bd9Sstevel@tonic-gate 	/* FUTUREWORK: test PhysicalAddress for > 32 bits */
600db2bae30SDana Myers 	return (psm_map_new((paddr_t)PhysicalAddress,
601db2bae30SDana Myers 	    (size_t)Size, PSM_PROT_WRITE | PSM_PROT_READ));
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate void
6057c478bd9Sstevel@tonic-gate AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Size)
6067c478bd9Sstevel@tonic-gate {
607b9bfdccdSStuart Maybee 	/*
608b9bfdccdSStuart Maybee 	 * Check if trying to unmap dummy ioapic address.
609b9bfdccdSStuart Maybee 	 */
610b9bfdccdSStuart Maybee 	if (LogicalAddress == dummy_ioapicadr)
611b9bfdccdSStuart Maybee 		return;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	psm_unmap((caddr_t)LogicalAddress, (size_t)Size);
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6177c478bd9Sstevel@tonic-gate ACPI_STATUS
6187c478bd9Sstevel@tonic-gate AcpiOsGetPhysicalAddress(void *LogicalAddress,
6197c478bd9Sstevel@tonic-gate 			ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	/* UNIMPLEMENTED: not invoked by ACPI CA code */
62303335b7aSmyers 	return (AE_NOT_IMPLEMENTED);
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate ACPI_OSD_HANDLER acpi_isr;
6287c478bd9Sstevel@tonic-gate void *acpi_isr_context;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate uint_t
6317c478bd9Sstevel@tonic-gate acpi_wrapper_isr(char *arg)
6327c478bd9Sstevel@tonic-gate {
6337c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(arg))
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	int	status;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	status = (*acpi_isr)(acpi_isr_context);
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	if (status == ACPI_INTERRUPT_HANDLED) {
6407c478bd9Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
6417c478bd9Sstevel@tonic-gate 	} else {
6427c478bd9Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
6437c478bd9Sstevel@tonic-gate 	}
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
646186507a7Smyers static int acpi_intr_hooked = 0;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate ACPI_STATUS
6497c478bd9Sstevel@tonic-gate AcpiOsInstallInterruptHandler(UINT32 InterruptNumber,
6507c478bd9Sstevel@tonic-gate 		ACPI_OSD_HANDLER ServiceRoutine,
6517c478bd9Sstevel@tonic-gate 		void *Context)
6527c478bd9Sstevel@tonic-gate {
65327f7c583Smyers 	_NOTE(ARGUNUSED(InterruptNumber))
65427f7c583Smyers 
6557c478bd9Sstevel@tonic-gate 	int retval;
656450d6964Smyers 	int sci_vect;
657450d6964Smyers 	iflag_t sci_flags;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	acpi_isr = ServiceRoutine;
6607c478bd9Sstevel@tonic-gate 	acpi_isr_context = Context;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	/*
663450d6964Smyers 	 * Get SCI (adjusted for PIC/APIC mode if necessary)
6647c478bd9Sstevel@tonic-gate 	 */
665450d6964Smyers 	if (acpica_get_sci(&sci_vect, &sci_flags) != AE_OK) {
666450d6964Smyers 		return (AE_ERROR);
667450d6964Smyers 	}
668450d6964Smyers 
669450d6964Smyers #ifdef	DEBUG
670450d6964Smyers 	cmn_err(CE_NOTE, "!acpica: attaching SCI %d", sci_vect);
671450d6964Smyers #endif
672450d6964Smyers 
6737c478bd9Sstevel@tonic-gate 	retval = add_avintr(NULL, SCI_IPL, (avfunc)acpi_wrapper_isr,
6745dff2f34Smyers 	    "ACPI SCI", sci_vect, NULL, NULL, NULL, NULL);
6757c478bd9Sstevel@tonic-gate 	if (retval) {
676186507a7Smyers 		acpi_intr_hooked = 1;
6777c478bd9Sstevel@tonic-gate 		return (AE_OK);
678186507a7Smyers 	} else
6797c478bd9Sstevel@tonic-gate 		return (AE_BAD_PARAMETER);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate ACPI_STATUS
6837c478bd9Sstevel@tonic-gate AcpiOsRemoveInterruptHandler(UINT32 InterruptNumber,
6847c478bd9Sstevel@tonic-gate 			ACPI_OSD_HANDLER ServiceRoutine)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(ServiceRoutine))
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate #ifdef	DEBUG
6897c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!acpica: detaching SCI %d", InterruptNumber);
6907c478bd9Sstevel@tonic-gate #endif
691186507a7Smyers 	if (acpi_intr_hooked) {
6927c478bd9Sstevel@tonic-gate 		rem_avintr(NULL, LOCK_LEVEL - 1, (avfunc)acpi_wrapper_isr,
6935dff2f34Smyers 		    InterruptNumber);
694186507a7Smyers 		acpi_intr_hooked = 0;
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 	return (AE_OK);
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 
70027f7c583Smyers ACPI_THREAD_ID
7017c478bd9Sstevel@tonic-gate AcpiOsGetThreadId(void)
7027c478bd9Sstevel@tonic-gate {
70327f7c583Smyers 	/*
704aa2aa9a6SDana Myers 	 * ACPI CA doesn't care what actual value is returned as long
705aa2aa9a6SDana Myers 	 * as it is non-zero and unique to each existing thread.
706aa2aa9a6SDana Myers 	 * ACPI CA assumes that thread ID is castable to a pointer,
707aa2aa9a6SDana Myers 	 * so we use the current thread pointer.
70827f7c583Smyers 	 */
709aa2aa9a6SDana Myers 	return (curthread);
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate /*
7137c478bd9Sstevel@tonic-gate  *
7147c478bd9Sstevel@tonic-gate  */
7157c478bd9Sstevel@tonic-gate ACPI_STATUS
71627f7c583Smyers AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK  Function,
71727f7c583Smyers     void *Context)
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	if (!acpica_eventq_init) {
7217c478bd9Sstevel@tonic-gate 		/*
7227c478bd9Sstevel@tonic-gate 		 * Create taskqs for event handling
7237c478bd9Sstevel@tonic-gate 		 */
7247c478bd9Sstevel@tonic-gate 		if (init_event_queues() != AE_OK)
7255dff2f34Smyers 			return (AE_ERROR);
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate 
72827f7c583Smyers 	if (ddi_taskq_dispatch(osl_eventq[Type], Function, Context,
72927f7c583Smyers 	    DDI_NOSLEEP) == DDI_FAILURE) {
7307c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7317c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!acpica: unable to dispatch event");
7327c478bd9Sstevel@tonic-gate #endif
7337c478bd9Sstevel@tonic-gate 		return (AE_ERROR);
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 	return (AE_OK);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate }
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate void
7407c478bd9Sstevel@tonic-gate AcpiOsSleep(ACPI_INTEGER Milliseconds)
7417c478bd9Sstevel@tonic-gate {
7427c478bd9Sstevel@tonic-gate 	/*
743137e2f25SGuoli Shu 	 * During kernel startup, before the first tick interrupt
744137e2f25SGuoli Shu 	 * has taken place, we can't call delay; very late in
745137e2f25SGuoli Shu 	 * kernel shutdown or suspend/resume, clock interrupts
746859dcd3dSmyers 	 * are blocked, so delay doesn't work then either.
747859dcd3dSmyers 	 * So we busy wait if lbolt == 0 (kernel startup)
748137e2f25SGuoli Shu 	 * or if acpica_use_safe_delay has been set to a
749137e2f25SGuoli Shu 	 * non-zero value.
7507c478bd9Sstevel@tonic-gate 	 */
751137e2f25SGuoli Shu 	if ((ddi_get_lbolt() == 0) || acpica_use_safe_delay)
7527c478bd9Sstevel@tonic-gate 		drv_usecwait(Milliseconds * 1000);
7537c478bd9Sstevel@tonic-gate 	else
7547c478bd9Sstevel@tonic-gate 		delay(drv_usectohz(Milliseconds * 1000));
7557c478bd9Sstevel@tonic-gate }
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate void
7587c478bd9Sstevel@tonic-gate AcpiOsStall(UINT32 Microseconds)
7597c478bd9Sstevel@tonic-gate {
7607c478bd9Sstevel@tonic-gate 	drv_usecwait(Microseconds);
7617c478bd9Sstevel@tonic-gate }
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate /*
7657c478bd9Sstevel@tonic-gate  * Implementation of "Windows 2001" compatible I/O permission map
7667c478bd9Sstevel@tonic-gate  *
7677c478bd9Sstevel@tonic-gate  */
7687c478bd9Sstevel@tonic-gate #define	OSL_IO_NONE	(0)
7697c478bd9Sstevel@tonic-gate #define	OSL_IO_READ	(1<<0)
7707c478bd9Sstevel@tonic-gate #define	OSL_IO_WRITE	(1<<1)
7717c478bd9Sstevel@tonic-gate #define	OSL_IO_RW	(OSL_IO_READ | OSL_IO_WRITE)
7727c478bd9Sstevel@tonic-gate #define	OSL_IO_TERM	(1<<2)
7737c478bd9Sstevel@tonic-gate #define	OSL_IO_DEFAULT	OSL_IO_RW
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate static struct io_perm  {
7767c478bd9Sstevel@tonic-gate 	ACPI_IO_ADDRESS	low;
7777c478bd9Sstevel@tonic-gate 	ACPI_IO_ADDRESS	high;
7787c478bd9Sstevel@tonic-gate 	uint8_t		perm;
7797c478bd9Sstevel@tonic-gate } osl_io_perm[] = {
7807c478bd9Sstevel@tonic-gate 	{ 0xcf8, 0xd00, OSL_IO_NONE | OSL_IO_TERM }
7817c478bd9Sstevel@tonic-gate };
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate /*
7857c478bd9Sstevel@tonic-gate  *
7867c478bd9Sstevel@tonic-gate  */
7877c478bd9Sstevel@tonic-gate static struct io_perm *
7887c478bd9Sstevel@tonic-gate osl_io_find_perm(ACPI_IO_ADDRESS addr)
7897c478bd9Sstevel@tonic-gate {
7907c478bd9Sstevel@tonic-gate 	struct io_perm *p;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	p = osl_io_perm;
7937c478bd9Sstevel@tonic-gate 	while (p != NULL) {
7947c478bd9Sstevel@tonic-gate 		if ((p->low <= addr) && (addr <= p->high))
7957c478bd9Sstevel@tonic-gate 			break;
7967c478bd9Sstevel@tonic-gate 		p = (p->perm & OSL_IO_TERM) ? NULL : p+1;
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	return (p);
8007c478bd9Sstevel@tonic-gate }
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate /*
8037c478bd9Sstevel@tonic-gate  *
8047c478bd9Sstevel@tonic-gate  */
8057c478bd9Sstevel@tonic-gate ACPI_STATUS
8067c478bd9Sstevel@tonic-gate AcpiOsReadPort(ACPI_IO_ADDRESS Address, UINT32 *Value, UINT32 Width)
8077c478bd9Sstevel@tonic-gate {
8087c478bd9Sstevel@tonic-gate 	struct io_perm *p;
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	/* verify permission */
8117c478bd9Sstevel@tonic-gate 	p = osl_io_find_perm(Address);
8127c478bd9Sstevel@tonic-gate 	if (p && (p->perm & OSL_IO_READ) == 0) {
8137c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!AcpiOsReadPort: %lx %u not permitted",
8145dff2f34Smyers 		    (long)Address, Width);
8157c478bd9Sstevel@tonic-gate 		*Value = 0xffffffff;
8167c478bd9Sstevel@tonic-gate 		return (AE_ERROR);
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	switch (Width) {
8207c478bd9Sstevel@tonic-gate 	case 8:
8217c478bd9Sstevel@tonic-gate 		*Value = inb(Address);
8227c478bd9Sstevel@tonic-gate 		break;
8237c478bd9Sstevel@tonic-gate 	case 16:
8247c478bd9Sstevel@tonic-gate 		*Value = inw(Address);
8257c478bd9Sstevel@tonic-gate 		break;
8267c478bd9Sstevel@tonic-gate 	case 32:
8277c478bd9Sstevel@tonic-gate 		*Value = inl(Address);
8287c478bd9Sstevel@tonic-gate 		break;
8297c478bd9Sstevel@tonic-gate 	default:
8307c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!AcpiOsReadPort: %lx %u failed",
8315dff2f34Smyers 		    (long)Address, Width);
8327c478bd9Sstevel@tonic-gate 		return (AE_BAD_PARAMETER);
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate 	return (AE_OK);
8357c478bd9Sstevel@tonic-gate }
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate ACPI_STATUS
8387c478bd9Sstevel@tonic-gate AcpiOsWritePort(ACPI_IO_ADDRESS Address, UINT32 Value, UINT32 Width)
8397c478bd9Sstevel@tonic-gate {
8407c478bd9Sstevel@tonic-gate 	struct io_perm *p;
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	/* verify permission */
8437c478bd9Sstevel@tonic-gate 	p = osl_io_find_perm(Address);
8447c478bd9Sstevel@tonic-gate 	if (p && (p->perm & OSL_IO_WRITE) == 0) {
8457c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!AcpiOsWritePort: %lx %u not permitted",
8467c478bd9Sstevel@tonic-gate 		    (long)Address, Width);
8477c478bd9Sstevel@tonic-gate 		return (AE_ERROR);
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	switch (Width) {
8517c478bd9Sstevel@tonic-gate 	case 8:
8527c478bd9Sstevel@tonic-gate 		outb(Address, Value);
8537c478bd9Sstevel@tonic-gate 		break;
8547c478bd9Sstevel@tonic-gate 	case 16:
8557c478bd9Sstevel@tonic-gate 		outw(Address, Value);
8567c478bd9Sstevel@tonic-gate 		break;
8577c478bd9Sstevel@tonic-gate 	case 32:
8587c478bd9Sstevel@tonic-gate 		outl(Address, Value);
8597c478bd9Sstevel@tonic-gate 		break;
8607c478bd9Sstevel@tonic-gate 	default:
8617c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!AcpiOsWritePort: %lx %u failed",
8627c478bd9Sstevel@tonic-gate 		    (long)Address, Width);
8637c478bd9Sstevel@tonic-gate 		return (AE_BAD_PARAMETER);
8647c478bd9Sstevel@tonic-gate 	}
8657c478bd9Sstevel@tonic-gate 	return (AE_OK);
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate /*
8707c478bd9Sstevel@tonic-gate  *
8717c478bd9Sstevel@tonic-gate  */
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate #define	OSL_RW(ptr, val, type, rw) \
8747c478bd9Sstevel@tonic-gate 	{ if (rw) *((type *)(ptr)) = *((type *) val); \
8757c478bd9Sstevel@tonic-gate 	    else *((type *) val) = *((type *)(ptr)); }
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate static void
8797c478bd9Sstevel@tonic-gate osl_rw_memory(ACPI_PHYSICAL_ADDRESS Address, UINT32 *Value,
8807c478bd9Sstevel@tonic-gate     UINT32 Width, int write)
8817c478bd9Sstevel@tonic-gate {
8827c478bd9Sstevel@tonic-gate 	size_t	maplen = Width / 8;
8837c478bd9Sstevel@tonic-gate 	caddr_t	ptr;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	ptr = psm_map_new((paddr_t)Address, maplen,
8867c478bd9Sstevel@tonic-gate 	    PSM_PROT_WRITE | PSM_PROT_READ);
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	switch (maplen) {
8897c478bd9Sstevel@tonic-gate 	case 1:
8907c478bd9Sstevel@tonic-gate 		OSL_RW(ptr, Value, uint8_t, write);
8917c478bd9Sstevel@tonic-gate 		break;
8927c478bd9Sstevel@tonic-gate 	case 2:
8937c478bd9Sstevel@tonic-gate 		OSL_RW(ptr, Value, uint16_t, write);
8947c478bd9Sstevel@tonic-gate 		break;
8957c478bd9Sstevel@tonic-gate 	case 4:
8967c478bd9Sstevel@tonic-gate 		OSL_RW(ptr, Value, uint32_t, write);
8977c478bd9Sstevel@tonic-gate 		break;
8987c478bd9Sstevel@tonic-gate 	default:
8997c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!osl_rw_memory: invalid size %d",
9007c478bd9Sstevel@tonic-gate 		    Width);
9017c478bd9Sstevel@tonic-gate 		break;
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	psm_unmap(ptr, maplen);
9057c478bd9Sstevel@tonic-gate }
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate ACPI_STATUS
9087c478bd9Sstevel@tonic-gate AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address,
9097c478bd9Sstevel@tonic-gate 		UINT32 *Value, UINT32 Width)
9107c478bd9Sstevel@tonic-gate {
9117c478bd9Sstevel@tonic-gate 	osl_rw_memory(Address, Value, Width, 0);
9127c478bd9Sstevel@tonic-gate 	return (AE_OK);
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate ACPI_STATUS
9167c478bd9Sstevel@tonic-gate AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address,
9177c478bd9Sstevel@tonic-gate 		UINT32 Value, UINT32 Width)
9187c478bd9Sstevel@tonic-gate {
9197c478bd9Sstevel@tonic-gate 	osl_rw_memory(Address, &Value, Width, 1);
9207c478bd9Sstevel@tonic-gate 	return (AE_OK);
9217c478bd9Sstevel@tonic-gate }
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate ACPI_STATUS
9257c478bd9Sstevel@tonic-gate AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register,
9267c478bd9Sstevel@tonic-gate 			void *Value, UINT32 Width)
9277c478bd9Sstevel@tonic-gate {
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	switch (Width) {
9307c478bd9Sstevel@tonic-gate 	case 8:
9317c478bd9Sstevel@tonic-gate 		*((UINT64 *)Value) = (UINT64)(*pci_getb_func)
9325dff2f34Smyers 		    (PciId->Bus, PciId->Device, PciId->Function, Register);
9337c478bd9Sstevel@tonic-gate 		break;
9347c478bd9Sstevel@tonic-gate 	case 16:
9357c478bd9Sstevel@tonic-gate 		*((UINT64 *)Value) = (UINT64)(*pci_getw_func)
9365dff2f34Smyers 		    (PciId->Bus, PciId->Device, PciId->Function, Register);
9377c478bd9Sstevel@tonic-gate 		break;
9387c478bd9Sstevel@tonic-gate 	case 32:
9397c478bd9Sstevel@tonic-gate 		*((UINT64 *)Value) = (UINT64)(*pci_getl_func)
9405dff2f34Smyers 		    (PciId->Bus, PciId->Device, PciId->Function, Register);
9417c478bd9Sstevel@tonic-gate 		break;
9427c478bd9Sstevel@tonic-gate 	case 64:
9437c478bd9Sstevel@tonic-gate 	default:
9447c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!AcpiOsReadPciConfiguration: %x %u failed",
9457c478bd9Sstevel@tonic-gate 		    Register, Width);
9467c478bd9Sstevel@tonic-gate 		return (AE_BAD_PARAMETER);
9477c478bd9Sstevel@tonic-gate 	}
9487c478bd9Sstevel@tonic-gate 	return (AE_OK);
9497c478bd9Sstevel@tonic-gate }
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate /*
9527c478bd9Sstevel@tonic-gate  *
9537c478bd9Sstevel@tonic-gate  */
9547c478bd9Sstevel@tonic-gate int acpica_write_pci_config_ok = 1;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate ACPI_STATUS
9577c478bd9Sstevel@tonic-gate AcpiOsWritePciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register,
9587c478bd9Sstevel@tonic-gate 		ACPI_INTEGER Value, UINT32 Width)
9597c478bd9Sstevel@tonic-gate {
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	if (!acpica_write_pci_config_ok) {
9627c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "!write to PCI cfg %x/%x/%x %x"
9637c478bd9Sstevel@tonic-gate 		    " %lx %d not permitted", PciId->Bus, PciId->Device,
9647c478bd9Sstevel@tonic-gate 		    PciId->Function, Register, (long)Value, Width);
9657c478bd9Sstevel@tonic-gate 		return (AE_OK);
9667c478bd9Sstevel@tonic-gate 	}
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	switch (Width) {
9697c478bd9Sstevel@tonic-gate 	case 8:
9707c478bd9Sstevel@tonic-gate 		(*pci_putb_func)(PciId->Bus, PciId->Device, PciId->Function,
9715dff2f34Smyers 		    Register, (uint8_t)Value);
9727c478bd9Sstevel@tonic-gate 		break;
9737c478bd9Sstevel@tonic-gate 	case 16:
9747c478bd9Sstevel@tonic-gate 		(*pci_putw_func)(PciId->Bus, PciId->Device, PciId->Function,
9755dff2f34Smyers 		    Register, (uint16_t)Value);
9767c478bd9Sstevel@tonic-gate 		break;
9777c478bd9Sstevel@tonic-gate 	case 32:
9787c478bd9Sstevel@tonic-gate 		(*pci_putl_func)(PciId->Bus, PciId->Device, PciId->Function,
9795dff2f34Smyers 		    Register, (uint32_t)Value);
9807c478bd9Sstevel@tonic-gate 		break;
9817c478bd9Sstevel@tonic-gate 	case 64:
9827c478bd9Sstevel@tonic-gate 	default:
9837c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!AcpiOsWritePciConfiguration: %x %u failed",
9847c478bd9Sstevel@tonic-gate 		    Register, Width);
9857c478bd9Sstevel@tonic-gate 		return (AE_BAD_PARAMETER);
9867c478bd9Sstevel@tonic-gate 	}
9877c478bd9Sstevel@tonic-gate 	return (AE_OK);
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate /*
9917c478bd9Sstevel@tonic-gate  * Called with ACPI_HANDLEs for both a PCI Config Space
9927c478bd9Sstevel@tonic-gate  * OpRegion and (what ACPI CA thinks is) the PCI device
99327f7c583Smyers  * to which this ConfigSpace OpRegion belongs.  Since
99427f7c583Smyers  * ACPI CA depends on a valid _BBN object being present
99527f7c583Smyers  * and this is not always true (one old x86 had broken _BBN),
99627f7c583Smyers  * we go ahead and get the correct PCI bus number using the
99727f7c583Smyers  * devinfo mapping (which compensates for broken _BBN).
9987c478bd9Sstevel@tonic-gate  *
9997c478bd9Sstevel@tonic-gate  * Default values for bus, segment, device and function are
10007c478bd9Sstevel@tonic-gate  * all 0 when ACPI CA can't figure them out.
10017c478bd9Sstevel@tonic-gate  *
100227f7c583Smyers  * Some BIOSes implement _BBN() by reading PCI config space
10035dff2f34Smyers  * on bus #0 - which means that we'll recurse when we attempt
100427f7c583Smyers  * to create the devinfo-to-ACPI map.  If Derive is called during
10055cff7825Smh  * scan_d2a_map, we don't translate the bus # and return.
10065cff7825Smh  *
10075cff7825Smh  * We get the parent of the OpRegion, which must be a PCI
10085cff7825Smh  * node, fetch the associated devinfo node and snag the
10095cff7825Smh  * b/d/f from it.
10107c478bd9Sstevel@tonic-gate  */
10117c478bd9Sstevel@tonic-gate void
10127c478bd9Sstevel@tonic-gate AcpiOsDerivePciId(ACPI_HANDLE rhandle, ACPI_HANDLE chandle,
10137c478bd9Sstevel@tonic-gate 		ACPI_PCI_ID **PciId)
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate 	ACPI_HANDLE handle;
10165cff7825Smh 	dev_info_t *dip;
10175cff7825Smh 	int bus, device, func, devfn;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 
102027f7c583Smyers 	/*
10215cff7825Smh 	 * See above - avoid recursing during scanning_d2a_map.
102227f7c583Smyers 	 */
10235cff7825Smh 	if (scanning_d2a_map)
10247c478bd9Sstevel@tonic-gate 		return;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	/*
10275cff7825Smh 	 * Get the OpRegion's parent
10287c478bd9Sstevel@tonic-gate 	 */
10297c478bd9Sstevel@tonic-gate 	if (AcpiGetParent(chandle, &handle) != AE_OK)
10307c478bd9Sstevel@tonic-gate 		return;
10317c478bd9Sstevel@tonic-gate 
10325cff7825Smh 	/*
10335cff7825Smh 	 * If we've mapped the ACPI node to the devinfo
10345cff7825Smh 	 * tree, use the devinfo reg property
10355cff7825Smh 	 */
10365cff7825Smh 	if (acpica_get_devinfo(handle, &dip) == AE_OK) {
10375cff7825Smh 		(void) acpica_get_bdf(dip, &bus, &device, &func);
10385cff7825Smh 		(*PciId)->Bus = bus;
10395cff7825Smh 		(*PciId)->Device = device;
10405cff7825Smh 		(*PciId)->Function = func;
10415cff7825Smh 	} else if (acpica_eval_int(handle, "_ADR", &devfn) == AE_OK) {
10425cff7825Smh 		/* no devinfo node - just confirm the d/f */
10435cff7825Smh 		(*PciId)->Device = (devfn >> 16) & 0xFFFF;
10445cff7825Smh 		(*PciId)->Function = devfn & 0xFFFF;
10457c478bd9Sstevel@tonic-gate 	}
10467c478bd9Sstevel@tonic-gate }
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 
104927f7c583Smyers /*ARGSUSED*/
10507c478bd9Sstevel@tonic-gate BOOLEAN
10517c478bd9Sstevel@tonic-gate AcpiOsReadable(void *Pointer, ACPI_SIZE Length)
10527c478bd9Sstevel@tonic-gate {
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	/* Always says yes; all mapped memory assumed readable */
10557c478bd9Sstevel@tonic-gate 	return (1);
10567c478bd9Sstevel@tonic-gate }
10577c478bd9Sstevel@tonic-gate 
105827f7c583Smyers /*ARGSUSED*/
10597c478bd9Sstevel@tonic-gate BOOLEAN
10607c478bd9Sstevel@tonic-gate AcpiOsWritable(void *Pointer, ACPI_SIZE Length)
10617c478bd9Sstevel@tonic-gate {
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	/* Always says yes; all mapped memory assumed writable */
10647c478bd9Sstevel@tonic-gate 	return (1);
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate UINT64
10687c478bd9Sstevel@tonic-gate AcpiOsGetTimer(void)
10697c478bd9Sstevel@tonic-gate {
107027f7c583Smyers 	/* gethrtime() returns 1nS resolution; convert to 100nS granules */
107127f7c583Smyers 	return ((gethrtime() + 50) / 100);
107227f7c583Smyers }
10737c478bd9Sstevel@tonic-gate 
1074fa96bd91SMichael Corcoran static struct AcpiOSIFeature_s {
1075fa96bd91SMichael Corcoran 	uint64_t	control_flag;
1076fa96bd91SMichael Corcoran 	const char	*feature_name;
1077fa96bd91SMichael Corcoran } AcpiOSIFeatures[] = {
1078fa96bd91SMichael Corcoran 	{ ACPI_FEATURE_OSI_MODULE,	"Module Device" },
1079fa96bd91SMichael Corcoran 	{ 0,				"Processor Device" }
1080fa96bd91SMichael Corcoran };
1081fa96bd91SMichael Corcoran 
108227f7c583Smyers /*ARGSUSED*/
108327f7c583Smyers ACPI_STATUS
1084fa96bd91SMichael Corcoran AcpiOsValidateInterface(char *feature)
108527f7c583Smyers {
1086fa96bd91SMichael Corcoran 	int i;
1087fa96bd91SMichael Corcoran 
1088fa96bd91SMichael Corcoran 	ASSERT(feature != NULL);
1089fa96bd91SMichael Corcoran 	for (i = 0; i < sizeof (AcpiOSIFeatures) / sizeof (AcpiOSIFeatures[0]);
1090fa96bd91SMichael Corcoran 	    i++) {
1091fa96bd91SMichael Corcoran 		if (strcmp(feature, AcpiOSIFeatures[i].feature_name) != 0) {
1092fa96bd91SMichael Corcoran 			continue;
1093fa96bd91SMichael Corcoran 		}
1094fa96bd91SMichael Corcoran 		/* Check whether required core features are available. */
1095fa96bd91SMichael Corcoran 		if (AcpiOSIFeatures[i].control_flag != 0 &&
1096fa96bd91SMichael Corcoran 		    acpica_get_core_feature(AcpiOSIFeatures[i].control_flag) !=
1097fa96bd91SMichael Corcoran 		    AcpiOSIFeatures[i].control_flag) {
1098fa96bd91SMichael Corcoran 			break;
1099fa96bd91SMichael Corcoran 		}
1100fa96bd91SMichael Corcoran 		/* Feature supported. */
1101fa96bd91SMichael Corcoran 		return (AE_OK);
1102fa96bd91SMichael Corcoran 	}
1103fa96bd91SMichael Corcoran 
110427f7c583Smyers 	return (AE_SUPPORT);
110527f7c583Smyers }
11067c478bd9Sstevel@tonic-gate 
110727f7c583Smyers /*ARGSUSED*/
110827f7c583Smyers ACPI_STATUS
110927f7c583Smyers AcpiOsValidateAddress(UINT8 spaceid, ACPI_PHYSICAL_ADDRESS addr,
111027f7c583Smyers     ACPI_SIZE length)
111127f7c583Smyers {
111227f7c583Smyers 	return (AE_OK);
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate ACPI_STATUS
11167c478bd9Sstevel@tonic-gate AcpiOsSignal(UINT32 Function, void *Info)
11177c478bd9Sstevel@tonic-gate {
11187c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(Function, Info))
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	/* FUTUREWORK: debugger support */
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!OsSignal unimplemented");
11237c478bd9Sstevel@tonic-gate 	return (AE_OK);
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate void ACPI_INTERNAL_VAR_XFACE
11277c478bd9Sstevel@tonic-gate AcpiOsPrintf(const char *Format, ...)
11287c478bd9Sstevel@tonic-gate {
11297c478bd9Sstevel@tonic-gate 	va_list ap;
1130186507a7Smyers 
1131186507a7Smyers 	va_start(ap, Format);
1132186507a7Smyers 	AcpiOsVprintf(Format, ap);
1133186507a7Smyers 	va_end(ap);
1134186507a7Smyers }
1135186507a7Smyers 
1136186507a7Smyers /*
1137186507a7Smyers  * When != 0, sends output to console
1138186507a7Smyers  * Patchable with kmdb or /etc/system.
1139186507a7Smyers  */
1140186507a7Smyers int acpica_console_out = 0;
1141186507a7Smyers 
1142186507a7Smyers #define	ACPICA_OUTBUF_LEN	160
1143186507a7Smyers char	acpica_outbuf[ACPICA_OUTBUF_LEN];
1144186507a7Smyers int	acpica_outbuf_offset;
1145186507a7Smyers 
1146186507a7Smyers /*
1147186507a7Smyers  *
1148186507a7Smyers  */
1149186507a7Smyers static void
115087bb58d6Smyers acpica_pr_buf(char *buf)
1151186507a7Smyers {
1152186507a7Smyers 	char c, *bufp, *outp;
1153186507a7Smyers 	int	out_remaining;
1154186507a7Smyers 
1155186507a7Smyers 	/*
1156186507a7Smyers 	 * copy the supplied buffer into the output buffer
1157186507a7Smyers 	 * when we hit a '\n' or overflow the output buffer,
1158186507a7Smyers 	 * output and reset the output buffer
1159186507a7Smyers 	 */
1160186507a7Smyers 	bufp = buf;
1161186507a7Smyers 	outp = acpica_outbuf + acpica_outbuf_offset;
1162186507a7Smyers 	out_remaining = ACPICA_OUTBUF_LEN - acpica_outbuf_offset - 1;
1163186507a7Smyers 	while (c = *bufp++) {
1164186507a7Smyers 		*outp++ = c;
1165186507a7Smyers 		if (c == '\n' || --out_remaining == 0) {
1166186507a7Smyers 			*outp = '\0';
1167186507a7Smyers 			if (acpica_console_out)
1168186507a7Smyers 				printf(acpica_outbuf);
1169186507a7Smyers 			else
1170186507a7Smyers 				(void) strlog(0, 0, 0,
1171186507a7Smyers 				    SL_CONSOLE | SL_NOTE | SL_LOGONLY,
1172186507a7Smyers 				    acpica_outbuf);
1173186507a7Smyers 			acpica_outbuf_offset = 0;
1174186507a7Smyers 			outp = acpica_outbuf;
1175186507a7Smyers 			out_remaining = ACPICA_OUTBUF_LEN - 1;
1176186507a7Smyers 		}
1177186507a7Smyers 	}
1178186507a7Smyers 
1179186507a7Smyers 	acpica_outbuf_offset = outp - acpica_outbuf;
1180186507a7Smyers }
1181186507a7Smyers 
1182186507a7Smyers void
1183186507a7Smyers AcpiOsVprintf(const char *Format, va_list Args)
1184186507a7Smyers {
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	/*
118787bb58d6Smyers 	 * If AcpiOsInitialize() failed to allocate a string buffer,
118887bb58d6Smyers 	 * resort to vprintf().
11897c478bd9Sstevel@tonic-gate 	 */
119087bb58d6Smyers 	if (acpi_osl_pr_buffer == NULL) {
1191186507a7Smyers 		vprintf(Format, Args);
11927c478bd9Sstevel@tonic-gate 		return;
11937c478bd9Sstevel@tonic-gate 	}
119487bb58d6Smyers 
119587bb58d6Smyers 	/*
119687bb58d6Smyers 	 * It is possible that a very long debug output statement will
119787bb58d6Smyers 	 * be truncated; this is silently ignored.
119887bb58d6Smyers 	 */
119987bb58d6Smyers 	(void) vsnprintf(acpi_osl_pr_buffer, acpi_osl_pr_buflen, Format, Args);
120087bb58d6Smyers 	acpica_pr_buf(acpi_osl_pr_buffer);
12017c478bd9Sstevel@tonic-gate }
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate void
12047c478bd9Sstevel@tonic-gate AcpiOsRedirectOutput(void *Destination)
12057c478bd9Sstevel@tonic-gate {
12067c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(Destination))
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	/* FUTUREWORK: debugger support */
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate #ifdef	DEBUG
12117c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "!acpica: AcpiOsRedirectOutput called");
12127c478bd9Sstevel@tonic-gate #endif
12137c478bd9Sstevel@tonic-gate }
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate UINT32
12177c478bd9Sstevel@tonic-gate AcpiOsGetLine(char *Buffer)
12187c478bd9Sstevel@tonic-gate {
12197c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(Buffer))
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	/* FUTUREWORK: debugger support */
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	return (0);
12247c478bd9Sstevel@tonic-gate }
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate /*
12277c478bd9Sstevel@tonic-gate  * Device tree binding
12287c478bd9Sstevel@tonic-gate  */
1229fa96bd91SMichael Corcoran static ACPI_STATUS
1230fa96bd91SMichael Corcoran acpica_find_pcibus_walker(ACPI_HANDLE hdl, UINT32 lvl, void *ctxp, void **rvpp)
1231fa96bd91SMichael Corcoran {
1232fa96bd91SMichael Corcoran 	_NOTE(ARGUNUSED(lvl));
1233fa96bd91SMichael Corcoran 
1234fa96bd91SMichael Corcoran 	int sta, hid, bbn;
1235fa96bd91SMichael Corcoran 	int busno = (intptr_t)ctxp;
1236fa96bd91SMichael Corcoran 	ACPI_HANDLE *hdlp = (ACPI_HANDLE *)rvpp;
1237fa96bd91SMichael Corcoran 
1238fa96bd91SMichael Corcoran 	/* Check whether device exists. */
1239fa96bd91SMichael Corcoran 	if (ACPI_SUCCESS(acpica_eval_int(hdl, "_STA", &sta)) &&
1240fa96bd91SMichael Corcoran 	    !(sta & (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_FUNCTIONING))) {
1241fa96bd91SMichael Corcoran 		/*
1242fa96bd91SMichael Corcoran 		 * Skip object if device doesn't exist.
1243fa96bd91SMichael Corcoran 		 * According to ACPI Spec,
1244fa96bd91SMichael Corcoran 		 * 1) setting either bit 0 or bit 3 means that device exists.
1245fa96bd91SMichael Corcoran 		 * 2) Absence of _STA method means all status bits set.
1246fa96bd91SMichael Corcoran 		 */
1247fa96bd91SMichael Corcoran 		return (AE_CTRL_DEPTH);
1248fa96bd91SMichael Corcoran 	}
1249fa96bd91SMichael Corcoran 
1250fa96bd91SMichael Corcoran 	if (ACPI_FAILURE(acpica_eval_hid(hdl, "_HID", &hid)) ||
1251fa96bd91SMichael Corcoran 	    (hid != HID_PCI_BUS && hid != HID_PCI_EXPRESS_BUS)) {
1252fa96bd91SMichael Corcoran 		/* Non PCI/PCIe host bridge. */
1253fa96bd91SMichael Corcoran 		return (AE_OK);
1254fa96bd91SMichael Corcoran 	}
1255fa96bd91SMichael Corcoran 
1256fa96bd91SMichael Corcoran 	if (acpi_has_broken_bbn) {
1257fa96bd91SMichael Corcoran 		ACPI_BUFFER rb;
1258fa96bd91SMichael Corcoran 		rb.Pointer = NULL;
1259fa96bd91SMichael Corcoran 		rb.Length = ACPI_ALLOCATE_BUFFER;
1260fa96bd91SMichael Corcoran 
1261fa96bd91SMichael Corcoran 		/* Decree _BBN == n from PCI<n> */
1262fa96bd91SMichael Corcoran 		if (AcpiGetName(hdl, ACPI_SINGLE_NAME, &rb) != AE_OK) {
1263fa96bd91SMichael Corcoran 			return (AE_CTRL_TERMINATE);
1264fa96bd91SMichael Corcoran 		}
1265fa96bd91SMichael Corcoran 		bbn = ((char *)rb.Pointer)[3] - '0';
1266fa96bd91SMichael Corcoran 		AcpiOsFree(rb.Pointer);
1267fa96bd91SMichael Corcoran 		if (bbn == busno || busno == 0) {
1268fa96bd91SMichael Corcoran 			*hdlp = hdl;
1269fa96bd91SMichael Corcoran 			return (AE_CTRL_TERMINATE);
1270fa96bd91SMichael Corcoran 		}
1271fa96bd91SMichael Corcoran 	} else if (ACPI_SUCCESS(acpica_eval_int(hdl, "_BBN", &bbn))) {
1272fa96bd91SMichael Corcoran 		if (bbn == busno) {
1273fa96bd91SMichael Corcoran 			*hdlp = hdl;
1274fa96bd91SMichael Corcoran 			return (AE_CTRL_TERMINATE);
1275fa96bd91SMichael Corcoran 		}
1276fa96bd91SMichael Corcoran 	} else if (busno == 0) {
1277fa96bd91SMichael Corcoran 		*hdlp = hdl;
1278fa96bd91SMichael Corcoran 		return (AE_CTRL_TERMINATE);
1279fa96bd91SMichael Corcoran 	}
1280fa96bd91SMichael Corcoran 
1281fa96bd91SMichael Corcoran 	return (AE_CTRL_DEPTH);
1282fa96bd91SMichael Corcoran }
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate static int
12857c478bd9Sstevel@tonic-gate acpica_find_pcibus(int busno, ACPI_HANDLE *rh)
12867c478bd9Sstevel@tonic-gate {
12877c478bd9Sstevel@tonic-gate 	ACPI_HANDLE sbobj, busobj;
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	/* initialize static flag by querying ACPI namespace for bug */
12907c478bd9Sstevel@tonic-gate 	if (acpi_has_broken_bbn == -1)
12917c478bd9Sstevel@tonic-gate 		acpi_has_broken_bbn = acpica_query_bbn_problem();
12927c478bd9Sstevel@tonic-gate 
1293fa96bd91SMichael Corcoran 	if (ACPI_SUCCESS(AcpiGetHandle(NULL, "\\_SB", &sbobj))) {
1294fa96bd91SMichael Corcoran 		busobj = NULL;
1295fa96bd91SMichael Corcoran 		(void) AcpiWalkNamespace(ACPI_TYPE_DEVICE, sbobj, UINT32_MAX,
1296fa96bd91SMichael Corcoran 		    acpica_find_pcibus_walker, (void *)(intptr_t)busno,
1297fa96bd91SMichael Corcoran 		    (void **)&busobj);
1298fa96bd91SMichael Corcoran 		if (busobj != NULL) {
1299fa96bd91SMichael Corcoran 			*rh = busobj;
1300fa96bd91SMichael Corcoran 			return (AE_OK);
13017c478bd9Sstevel@tonic-gate 		}
13027c478bd9Sstevel@tonic-gate 	}
1303fa96bd91SMichael Corcoran 
13047c478bd9Sstevel@tonic-gate 	return (AE_ERROR);
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate 
1307fa96bd91SMichael Corcoran static ACPI_STATUS
1308fa96bd91SMichael Corcoran acpica_query_bbn_walker(ACPI_HANDLE hdl, UINT32 lvl, void *ctxp, void **rvpp)
1309fa96bd91SMichael Corcoran {
1310fa96bd91SMichael Corcoran 	_NOTE(ARGUNUSED(lvl));
1311fa96bd91SMichael Corcoran 	_NOTE(ARGUNUSED(rvpp));
1312fa96bd91SMichael Corcoran 
1313fa96bd91SMichael Corcoran 	int sta, hid, bbn;
1314fa96bd91SMichael Corcoran 	int *cntp = (int *)ctxp;
1315fa96bd91SMichael Corcoran 
1316fa96bd91SMichael Corcoran 	/* Check whether device exists. */
1317fa96bd91SMichael Corcoran 	if (ACPI_SUCCESS(acpica_eval_int(hdl, "_STA", &sta)) &&
1318fa96bd91SMichael Corcoran 	    !(sta & (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_FUNCTIONING))) {
1319fa96bd91SMichael Corcoran 		/*
1320fa96bd91SMichael Corcoran 		 * Skip object if device doesn't exist.
1321fa96bd91SMichael Corcoran 		 * According to ACPI Spec,
1322fa96bd91SMichael Corcoran 		 * 1) setting either bit 0 or bit 3 means that device exists.
1323fa96bd91SMichael Corcoran 		 * 2) Absence of _STA method means all status bits set.
1324fa96bd91SMichael Corcoran 		 */
1325fa96bd91SMichael Corcoran 		return (AE_CTRL_DEPTH);
1326fa96bd91SMichael Corcoran 	}
1327fa96bd91SMichael Corcoran 
1328fa96bd91SMichael Corcoran 	if (ACPI_FAILURE(acpica_eval_hid(hdl, "_HID", &hid)) ||
1329fa96bd91SMichael Corcoran 	    (hid != HID_PCI_BUS && hid != HID_PCI_EXPRESS_BUS)) {
1330fa96bd91SMichael Corcoran 		/* Non PCI/PCIe host bridge. */
1331fa96bd91SMichael Corcoran 		return (AE_OK);
1332fa96bd91SMichael Corcoran 	} else if (ACPI_SUCCESS(acpica_eval_int(hdl, "_BBN", &bbn)) &&
1333fa96bd91SMichael Corcoran 	    bbn == 0 && ++(*cntp) > 1) {
1334fa96bd91SMichael Corcoran 		/*
1335fa96bd91SMichael Corcoran 		 * If we find more than one bus with a 0 _BBN
1336fa96bd91SMichael Corcoran 		 * we have the problem that BigBear's BIOS shows
1337fa96bd91SMichael Corcoran 		 */
1338fa96bd91SMichael Corcoran 		return (AE_CTRL_TERMINATE);
1339fa96bd91SMichael Corcoran 	} else {
1340fa96bd91SMichael Corcoran 		/*
1341fa96bd91SMichael Corcoran 		 * Skip children of PCI/PCIe host bridge.
1342fa96bd91SMichael Corcoran 		 */
1343fa96bd91SMichael Corcoran 		return (AE_CTRL_DEPTH);
1344fa96bd91SMichael Corcoran 	}
1345fa96bd91SMichael Corcoran }
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate /*
13487c478bd9Sstevel@tonic-gate  * Look for ACPI problem where _BBN is zero for multiple PCI buses
13497c478bd9Sstevel@tonic-gate  * This is a clear ACPI bug, but we have a workaround in acpica_find_pcibus()
13507c478bd9Sstevel@tonic-gate  * below if it exists.
13517c478bd9Sstevel@tonic-gate  */
13527c478bd9Sstevel@tonic-gate static int
13537c478bd9Sstevel@tonic-gate acpica_query_bbn_problem(void)
13547c478bd9Sstevel@tonic-gate {
1355fa96bd91SMichael Corcoran 	ACPI_HANDLE sbobj;
13567c478bd9Sstevel@tonic-gate 	int zerobbncnt;
1357fa96bd91SMichael Corcoran 	void *rv;
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	zerobbncnt = 0;
1360fa96bd91SMichael Corcoran 	if (ACPI_SUCCESS(AcpiGetHandle(NULL, "\\_SB", &sbobj))) {
1361fa96bd91SMichael Corcoran 		(void) AcpiWalkNamespace(ACPI_TYPE_DEVICE, sbobj, UINT32_MAX,
1362fa96bd91SMichael Corcoran 		    acpica_query_bbn_walker, &zerobbncnt, &rv);
13637c478bd9Sstevel@tonic-gate 	}
1364fa96bd91SMichael Corcoran 
1365fa96bd91SMichael Corcoran 	return (zerobbncnt > 1 ? 1 : 0);
13667c478bd9Sstevel@tonic-gate }
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate static const char hextab[] = "0123456789ABCDEF";
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate static int
13717c478bd9Sstevel@tonic-gate hexdig(int c)
13727c478bd9Sstevel@tonic-gate {
13737c478bd9Sstevel@tonic-gate 	/*
13747c478bd9Sstevel@tonic-gate 	 *  Get hex digit:
13757c478bd9Sstevel@tonic-gate 	 *
13767c478bd9Sstevel@tonic-gate 	 *  Returns the 4-bit hex digit named by the input character.  Returns
13777c478bd9Sstevel@tonic-gate 	 *  zero if the input character is not valid hex!
13787c478bd9Sstevel@tonic-gate 	 */
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 	int x = ((c < 'a') || (c > 'z')) ? c : (c - ' ');
13817c478bd9Sstevel@tonic-gate 	int j = sizeof (hextab);
13827c478bd9Sstevel@tonic-gate 
13835dff2f34Smyers 	while (--j && (x != hextab[j])) {
13845dff2f34Smyers 	}
13857c478bd9Sstevel@tonic-gate 	return (j);
13867c478bd9Sstevel@tonic-gate }
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate static int
13897c478bd9Sstevel@tonic-gate CompressEisaID(char *np)
13907c478bd9Sstevel@tonic-gate {
13917c478bd9Sstevel@tonic-gate 	/*
13927c478bd9Sstevel@tonic-gate 	 *  Compress an EISA device name:
13937c478bd9Sstevel@tonic-gate 	 *
13947c478bd9Sstevel@tonic-gate 	 *  This routine converts a 7-byte ASCII device name into the 4-byte
13957c478bd9Sstevel@tonic-gate 	 *  compressed form used by EISA (50 bytes of ROM to save 1 byte of
13967c478bd9Sstevel@tonic-gate 	 *  NV-RAM!)
13977c478bd9Sstevel@tonic-gate 	 */
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	union { char octets[4]; int retval; } myu;
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	myu.octets[0] = ((np[0] & 0x1F) << 2) + ((np[1] >> 3) & 0x03);
14027c478bd9Sstevel@tonic-gate 	myu.octets[1] = ((np[1] & 0x07) << 5) + (np[2] & 0x1F);
14037c478bd9Sstevel@tonic-gate 	myu.octets[2] = (hexdig(np[3]) << 4) + hexdig(np[4]);
14047c478bd9Sstevel@tonic-gate 	myu.octets[3] = (hexdig(np[5]) << 4) + hexdig(np[6]);
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	return (myu.retval);
14077c478bd9Sstevel@tonic-gate }
14087c478bd9Sstevel@tonic-gate 
14095cff7825Smh ACPI_STATUS
14107c478bd9Sstevel@tonic-gate acpica_eval_int(ACPI_HANDLE dev, char *method, int *rint)
14117c478bd9Sstevel@tonic-gate {
14127c478bd9Sstevel@tonic-gate 	ACPI_STATUS status;
14137c478bd9Sstevel@tonic-gate 	ACPI_BUFFER rb;
14147c478bd9Sstevel@tonic-gate 	ACPI_OBJECT ro;
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	rb.Pointer = &ro;
14177c478bd9Sstevel@tonic-gate 	rb.Length = sizeof (ro);
14187c478bd9Sstevel@tonic-gate 	if ((status = AcpiEvaluateObjectTyped(dev, method, NULL, &rb,
14195dff2f34Smyers 	    ACPI_TYPE_INTEGER)) == AE_OK)
14207c478bd9Sstevel@tonic-gate 		*rint = ro.Integer.Value;
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 	return (status);
14237c478bd9Sstevel@tonic-gate }
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate static int
14267c478bd9Sstevel@tonic-gate acpica_eval_hid(ACPI_HANDLE dev, char *method, int *rint)
14277c478bd9Sstevel@tonic-gate {
14287c478bd9Sstevel@tonic-gate 	ACPI_BUFFER rb;
14297c478bd9Sstevel@tonic-gate 	ACPI_OBJECT *rv;
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	rb.Pointer = NULL;
14327c478bd9Sstevel@tonic-gate 	rb.Length = ACPI_ALLOCATE_BUFFER;
1433db2bae30SDana Myers 	if (AcpiEvaluateObject(dev, method, NULL, &rb) == AE_OK &&
1434db2bae30SDana Myers 	    rb.Length != 0) {
14357c478bd9Sstevel@tonic-gate 		rv = rb.Pointer;
14367c478bd9Sstevel@tonic-gate 		if (rv->Type == ACPI_TYPE_INTEGER) {
14377c478bd9Sstevel@tonic-gate 			*rint = rv->Integer.Value;
14387c478bd9Sstevel@tonic-gate 			AcpiOsFree(rv);
14397c478bd9Sstevel@tonic-gate 			return (AE_OK);
14407c478bd9Sstevel@tonic-gate 		} else if (rv->Type == ACPI_TYPE_STRING) {
14417c478bd9Sstevel@tonic-gate 			char *stringData;
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 			/* Convert the string into an EISA ID */
14447c478bd9Sstevel@tonic-gate 			if (rv->String.Pointer == NULL) {
14457c478bd9Sstevel@tonic-gate 				AcpiOsFree(rv);
14467c478bd9Sstevel@tonic-gate 				return (AE_ERROR);
14477c478bd9Sstevel@tonic-gate 			}
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 			stringData = rv->String.Pointer;
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 			/*
14527c478bd9Sstevel@tonic-gate 			 * If the string is an EisaID, it must be 7
14537c478bd9Sstevel@tonic-gate 			 * characters; if it's an ACPI ID, it will be 8
14547c478bd9Sstevel@tonic-gate 			 * (and we don't care about ACPI ids here).
14557c478bd9Sstevel@tonic-gate 			 */
14567c478bd9Sstevel@tonic-gate 			if (strlen(stringData) != 7) {
14577c478bd9Sstevel@tonic-gate 				AcpiOsFree(rv);
14587c478bd9Sstevel@tonic-gate 				return (AE_ERROR);
14597c478bd9Sstevel@tonic-gate 			}
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 			*rint = CompressEisaID(stringData);
14627c478bd9Sstevel@tonic-gate 			AcpiOsFree(rv);
14637c478bd9Sstevel@tonic-gate 			return (AE_OK);
14647c478bd9Sstevel@tonic-gate 		} else
14657c478bd9Sstevel@tonic-gate 			AcpiOsFree(rv);
14667c478bd9Sstevel@tonic-gate 	}
14677c478bd9Sstevel@tonic-gate 	return (AE_ERROR);
14687c478bd9Sstevel@tonic-gate }
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate /*
14715cff7825Smh  * Create linkage between devinfo nodes and ACPI nodes
14727c478bd9Sstevel@tonic-gate  */
1473fa96bd91SMichael Corcoran ACPI_STATUS
14745cff7825Smh acpica_tag_devinfo(dev_info_t *dip, ACPI_HANDLE acpiobj)
14757c478bd9Sstevel@tonic-gate {
14765cff7825Smh 	ACPI_STATUS status;
14775cff7825Smh 	ACPI_BUFFER rb;
14787c478bd9Sstevel@tonic-gate 
14795cff7825Smh 	/*
14805cff7825Smh 	 * Tag the devinfo node with the ACPI name
14815cff7825Smh 	 */
14825cff7825Smh 	rb.Pointer = NULL;
14835cff7825Smh 	rb.Length = ACPI_ALLOCATE_BUFFER;
1484fa96bd91SMichael Corcoran 	status = AcpiGetName(acpiobj, ACPI_FULL_PATHNAME, &rb);
1485fa96bd91SMichael Corcoran 	if (ACPI_FAILURE(status)) {
1486fa96bd91SMichael Corcoran 		cmn_err(CE_WARN, "acpica: could not get ACPI path!");
1487fa96bd91SMichael Corcoran 	} else {
14885cff7825Smh 		(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
14895cff7825Smh 		    "acpi-namespace", (char *)rb.Pointer);
14905cff7825Smh 		AcpiOsFree(rb.Pointer);
14917c478bd9Sstevel@tonic-gate 
1492fa96bd91SMichael Corcoran 		/*
1493fa96bd91SMichael Corcoran 		 * Tag the ACPI node with the dip
1494fa96bd91SMichael Corcoran 		 */
1495fa96bd91SMichael Corcoran 		status = acpica_set_devinfo(acpiobj, dip);
1496fa96bd91SMichael Corcoran 		ASSERT(ACPI_SUCCESS(status));
14975cff7825Smh 	}
14985cff7825Smh 
1499fa96bd91SMichael Corcoran 	return (status);
1500fa96bd91SMichael Corcoran }
15015cff7825Smh 
1502fa96bd91SMichael Corcoran /*
1503fa96bd91SMichael Corcoran  * Destroy linkage between devinfo nodes and ACPI nodes
1504fa96bd91SMichael Corcoran  */
1505fa96bd91SMichael Corcoran ACPI_STATUS
1506fa96bd91SMichael Corcoran acpica_untag_devinfo(dev_info_t *dip, ACPI_HANDLE acpiobj)
1507fa96bd91SMichael Corcoran {
1508fa96bd91SMichael Corcoran 	(void) acpica_unset_devinfo(acpiobj);
1509fa96bd91SMichael Corcoran 	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "acpi-namespace");
15107c478bd9Sstevel@tonic-gate 
1511fa96bd91SMichael Corcoran 	return (AE_OK);
15127c478bd9Sstevel@tonic-gate }
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate /*
1515495256c0Smh  * Return the ACPI device node matching the CPU dev_info node.
15167c478bd9Sstevel@tonic-gate  */
15175cff7825Smh ACPI_STATUS
15180e751525SEric Saxe acpica_get_handle_cpu(int cpu_id, ACPI_HANDLE *rh)
15197c478bd9Sstevel@tonic-gate {
1520fa96bd91SMichael Corcoran 	int i;
1521fa96bd91SMichael Corcoran 
15225cff7825Smh 	/*
15235cff7825Smh 	 * if cpu_map itself is NULL, we're a uppc system and
15245cff7825Smh 	 * acpica_build_processor_map() hasn't been called yet.
15255cff7825Smh 	 * So call it here
15265cff7825Smh 	 */
15275cff7825Smh 	if (cpu_map == NULL) {
15285cff7825Smh 		(void) acpica_build_processor_map();
15295cff7825Smh 		if (cpu_map == NULL)
15305cff7825Smh 			return (AE_ERROR);
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate 
1533fa96bd91SMichael Corcoran 	if (cpu_id < 0) {
15345cff7825Smh 		return (AE_ERROR);
1535fa96bd91SMichael Corcoran 	}
15365cff7825Smh 
1537fa96bd91SMichael Corcoran 	/*
1538fa96bd91SMichael Corcoran 	 * search object with cpuid in cpu_map
1539fa96bd91SMichael Corcoran 	 */
1540fa96bd91SMichael Corcoran 	mutex_enter(&cpu_map_lock);
1541fa96bd91SMichael Corcoran 	for (i = 0; i < cpu_map_count; i++) {
1542fa96bd91SMichael Corcoran 		if (cpu_map[i]->cpu_id == cpu_id) {
1543fa96bd91SMichael Corcoran 			break;
1544fa96bd91SMichael Corcoran 		}
1545fa96bd91SMichael Corcoran 	}
1546fa96bd91SMichael Corcoran 	if (i >= cpu_map_count || (cpu_map[i]->obj == NULL)) {
1547fa96bd91SMichael Corcoran 		mutex_exit(&cpu_map_lock);
1548fa96bd91SMichael Corcoran 		return (AE_ERROR);
1549fa96bd91SMichael Corcoran 	}
15505cff7825Smh 	*rh = cpu_map[cpu_id]->obj;
1551fa96bd91SMichael Corcoran 	mutex_exit(&cpu_map_lock);
1552fa96bd91SMichael Corcoran 
15535cff7825Smh 	return (AE_OK);
15545cff7825Smh }
15555cff7825Smh 
15565cff7825Smh /*
15575cff7825Smh  * Determine if this object is a processor
15585cff7825Smh  */
15595cff7825Smh static ACPI_STATUS
15605cff7825Smh acpica_probe_processor(ACPI_HANDLE obj, UINT32 level, void *ctx, void **rv)
15615cff7825Smh {
15625cff7825Smh 	ACPI_STATUS status;
15635cff7825Smh 	ACPI_OBJECT_TYPE objtype;
15644b1ce0a9SDana Myers 	unsigned long acpi_id;
15655cff7825Smh 	ACPI_BUFFER rb;
15665cff7825Smh 
15675cff7825Smh 	if (AcpiGetType(obj, &objtype) != AE_OK)
15685cff7825Smh 		return (AE_OK);
15695cff7825Smh 
15705cff7825Smh 	if (objtype == ACPI_TYPE_PROCESSOR) {
15715cff7825Smh 		/* process a Processor */
15727c478bd9Sstevel@tonic-gate 		rb.Pointer = NULL;
15737c478bd9Sstevel@tonic-gate 		rb.Length = ACPI_ALLOCATE_BUFFER;
1574db2bae30SDana Myers 		status = AcpiEvaluateObjectTyped(obj, NULL, NULL, &rb,
1575db2bae30SDana Myers 		    ACPI_TYPE_PROCESSOR);
15765cff7825Smh 		if (status != AE_OK) {
15774b1ce0a9SDana Myers 			cmn_err(CE_WARN, "!acpica: error probing Processor");
15785cff7825Smh 			return (status);
15797c478bd9Sstevel@tonic-gate 		}
15805cff7825Smh 		acpi_id = ((ACPI_OBJECT *)rb.Pointer)->Processor.ProcId;
15815cff7825Smh 		AcpiOsFree(rb.Pointer);
15825cff7825Smh 	} else if (objtype == ACPI_TYPE_DEVICE) {
15835cff7825Smh 		/* process a processor Device */
1584b6917abeSmishra 		rb.Pointer = NULL;
1585b6917abeSmishra 		rb.Length = ACPI_ALLOCATE_BUFFER;
1586b6917abeSmishra 		status = AcpiGetObjectInfo(obj, &rb);
1587b6917abeSmishra 		if (status != AE_OK) {
1588b6917abeSmishra 			cmn_err(CE_WARN,
15894b1ce0a9SDana Myers 			    "!acpica: error probing Processor Device\n");
1590b6917abeSmishra 			return (status);
1591b6917abeSmishra 		}
1592b6917abeSmishra 		ASSERT(((ACPI_OBJECT *)rb.Pointer)->Type ==
1593b6917abeSmishra 		    ACPI_TYPE_DEVICE);
15944b1ce0a9SDana Myers 
15954b1ce0a9SDana Myers 		if (ddi_strtoul(
15964b1ce0a9SDana Myers 		    ((ACPI_DEVICE_INFO *)rb.Pointer)->UniqueId.Value,
15974b1ce0a9SDana Myers 		    NULL, 10, &acpi_id) != 0) {
15984b1ce0a9SDana Myers 			AcpiOsFree(rb.Pointer);
15994b1ce0a9SDana Myers 			cmn_err(CE_WARN,
16004b1ce0a9SDana Myers 			    "!acpica: error probing Processor Device _UID\n");
16014b1ce0a9SDana Myers 			return (AE_ERROR);
16024b1ce0a9SDana Myers 		}
1603b6917abeSmishra 		AcpiOsFree(rb.Pointer);
16047c478bd9Sstevel@tonic-gate 	}
1605fa96bd91SMichael Corcoran 	(void) acpica_add_processor_to_map(acpi_id, obj, UINT32_MAX);
16065cff7825Smh 
16075cff7825Smh 	return (AE_OK);
16087c478bd9Sstevel@tonic-gate }
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate static void
16115cff7825Smh scan_d2a_map(void)
16127c478bd9Sstevel@tonic-gate {
16137c478bd9Sstevel@tonic-gate 	dev_info_t *dip, *cdip;
16147c478bd9Sstevel@tonic-gate 	ACPI_HANDLE acpiobj;
16157c478bd9Sstevel@tonic-gate 	char *device_type_prop;
16167c478bd9Sstevel@tonic-gate 	int bus;
16177c478bd9Sstevel@tonic-gate 	static int map_error = 0;
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	if (map_error)
16207c478bd9Sstevel@tonic-gate 		return;
16217c478bd9Sstevel@tonic-gate 
16225cff7825Smh 	scanning_d2a_map = 1;
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	/*
16257c478bd9Sstevel@tonic-gate 	 * Find all child-of-root PCI buses, and find their corresponding
16267c478bd9Sstevel@tonic-gate 	 * ACPI child-of-root PCI nodes.  For each one, add to the
16277c478bd9Sstevel@tonic-gate 	 * d2a table.
16287c478bd9Sstevel@tonic-gate 	 */
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	for (dip = ddi_get_child(ddi_root_node());
16317c478bd9Sstevel@tonic-gate 	    dip != NULL;
16327c478bd9Sstevel@tonic-gate 	    dip = ddi_get_next_sibling(dip)) {
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 		/* prune non-PCI nodes */
1635fa96bd91SMichael Corcoran 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
1636fa96bd91SMichael Corcoran 		    DDI_PROP_DONTPASS,
16377c478bd9Sstevel@tonic-gate 		    "device_type", &device_type_prop) != DDI_PROP_SUCCESS)
16387c478bd9Sstevel@tonic-gate 			continue;
16397c478bd9Sstevel@tonic-gate 
164070025d76Sjohnny 		if ((strcmp("pci", device_type_prop) != 0) &&
164170025d76Sjohnny 		    (strcmp("pciex", device_type_prop) != 0)) {
16427c478bd9Sstevel@tonic-gate 			ddi_prop_free(device_type_prop);
16437c478bd9Sstevel@tonic-gate 			continue;
16447c478bd9Sstevel@tonic-gate 		}
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 		ddi_prop_free(device_type_prop);
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 		/*
16497c478bd9Sstevel@tonic-gate 		 * To get bus number of dip, get first child and get its
16507c478bd9Sstevel@tonic-gate 		 * bus number.  If NULL, just continue, because we don't
16517c478bd9Sstevel@tonic-gate 		 * care about bus nodes with no children anyway.
16527c478bd9Sstevel@tonic-gate 		 */
16537c478bd9Sstevel@tonic-gate 		if ((cdip = ddi_get_child(dip)) == NULL)
16547c478bd9Sstevel@tonic-gate 			continue;
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 		if (acpica_get_bdf(cdip, &bus, NULL, NULL) < 0) {
16577c478bd9Sstevel@tonic-gate #ifdef D2ADEBUG
16587c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "Can't get bus number of PCI child?");
16597c478bd9Sstevel@tonic-gate #endif
16607c478bd9Sstevel@tonic-gate 			map_error = 1;
16615cff7825Smh 			scanning_d2a_map = 0;
16625cff7825Smh 			d2a_done = 1;
16637c478bd9Sstevel@tonic-gate 			return;
16647c478bd9Sstevel@tonic-gate 		}
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 		if (acpica_find_pcibus(bus, &acpiobj) == AE_ERROR) {
16677c478bd9Sstevel@tonic-gate #ifdef D2ADEBUG
16687c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "No ACPI bus obj for bus %d?\n", bus);
16697c478bd9Sstevel@tonic-gate #endif
16707c478bd9Sstevel@tonic-gate 			map_error = 1;
16717c478bd9Sstevel@tonic-gate 			continue;
16727c478bd9Sstevel@tonic-gate 		}
16737c478bd9Sstevel@tonic-gate 
16745cff7825Smh 		acpica_tag_devinfo(dip, acpiobj);
16757c478bd9Sstevel@tonic-gate 
16765cff7825Smh 		/* call recursively to enumerate subtrees */
16775cff7825Smh 		scan_d2a_subtree(dip, acpiobj, bus);
16787c478bd9Sstevel@tonic-gate 	}
16795cff7825Smh 
16805cff7825Smh 	scanning_d2a_map = 0;
16815cff7825Smh 	d2a_done = 1;
16827c478bd9Sstevel@tonic-gate }
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate /*
16857c478bd9Sstevel@tonic-gate  * For all acpi child devices of acpiobj, find their matching
16867c478bd9Sstevel@tonic-gate  * dip under "dip" argument.  (matching means "matches dev/fn").
16877c478bd9Sstevel@tonic-gate  * bus is assumed to already be a match from caller, and is
16887c478bd9Sstevel@tonic-gate  * used here only to record in the d2a entry.  Recurse if necessary.
16897c478bd9Sstevel@tonic-gate  */
16907c478bd9Sstevel@tonic-gate static void
16915cff7825Smh scan_d2a_subtree(dev_info_t *dip, ACPI_HANDLE acpiobj, int bus)
16927c478bd9Sstevel@tonic-gate {
16937c478bd9Sstevel@tonic-gate 	int acpi_devfn, hid;
16947c478bd9Sstevel@tonic-gate 	ACPI_HANDLE acld;
16957c478bd9Sstevel@tonic-gate 	dev_info_t *dcld;
16967c478bd9Sstevel@tonic-gate 	int dcld_b, dcld_d, dcld_f;
16977c478bd9Sstevel@tonic-gate 	int dev, func;
16985cff7825Smh 	char *device_type_prop;
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 	acld = NULL;
1701186507a7Smyers 	while (AcpiGetNextObject(ACPI_TYPE_DEVICE, acpiobj, acld, &acld)
17027c478bd9Sstevel@tonic-gate 	    == AE_OK) {
17037c478bd9Sstevel@tonic-gate 		/* get the dev/func we're looking for in the devinfo tree */
17047c478bd9Sstevel@tonic-gate 		if (acpica_eval_int(acld, "_ADR", &acpi_devfn) != AE_OK)
17057c478bd9Sstevel@tonic-gate 			continue;
17067c478bd9Sstevel@tonic-gate 		dev = (acpi_devfn >> 16) & 0xFFFF;
17077c478bd9Sstevel@tonic-gate 		func = acpi_devfn & 0xFFFF;
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 		/* look through all the immediate children of dip */
17107c478bd9Sstevel@tonic-gate 		for (dcld = ddi_get_child(dip); dcld != NULL;
17117c478bd9Sstevel@tonic-gate 		    dcld = ddi_get_next_sibling(dcld)) {
17127c478bd9Sstevel@tonic-gate 			if (acpica_get_bdf(dcld, &dcld_b, &dcld_d, &dcld_f) < 0)
17137c478bd9Sstevel@tonic-gate 				continue;
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 			/* dev must match; function must match or wildcard */
17167c478bd9Sstevel@tonic-gate 			if (dcld_d != dev ||
17177c478bd9Sstevel@tonic-gate 			    (func != 0xFFFF && func != dcld_f))
17187c478bd9Sstevel@tonic-gate 				continue;
17197c478bd9Sstevel@tonic-gate 			bus = dcld_b;
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 			/* found a match, record it */
17225cff7825Smh 			acpica_tag_devinfo(dcld, acld);
17235cff7825Smh 
17245cff7825Smh 			/* if we find a bridge, recurse from here */
1725fa96bd91SMichael Corcoran 			if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dcld,
1726fa96bd91SMichael Corcoran 			    DDI_PROP_DONTPASS, "device_type",
1727fa96bd91SMichael Corcoran 			    &device_type_prop) == DDI_PROP_SUCCESS) {
17285cff7825Smh 				if ((strcmp("pci", device_type_prop) == 0) ||
17295cff7825Smh 				    (strcmp("pciex", device_type_prop) == 0))
17305cff7825Smh 					scan_d2a_subtree(dcld, acld, bus);
17315cff7825Smh 				ddi_prop_free(device_type_prop);
17325cff7825Smh 			}
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 			/* done finding a match, so break now */
17357c478bd9Sstevel@tonic-gate 			break;
17367c478bd9Sstevel@tonic-gate 		}
17378e56767dSsmall 	}
17387c478bd9Sstevel@tonic-gate }
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate /*
17417c478bd9Sstevel@tonic-gate  * Return bus/dev/fn for PCI dip (note: not the parent "pci" node).
17427c478bd9Sstevel@tonic-gate  */
17437c478bd9Sstevel@tonic-gate int
17447c478bd9Sstevel@tonic-gate acpica_get_bdf(dev_info_t *dip, int *bus, int *device, int *func)
17457c478bd9Sstevel@tonic-gate {
17467c478bd9Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
17477c478bd9Sstevel@tonic-gate 	int len;
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
17507c478bd9Sstevel@tonic-gate 	    "reg", (int **)&pci_rp, (uint_t *)&len) != DDI_SUCCESS)
17517c478bd9Sstevel@tonic-gate 		return (-1);
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	if (len < (sizeof (pci_regspec_t) / sizeof (int))) {
17547c478bd9Sstevel@tonic-gate 		ddi_prop_free(pci_rp);
17557c478bd9Sstevel@tonic-gate 		return (-1);
17567c478bd9Sstevel@tonic-gate 	}
17577c478bd9Sstevel@tonic-gate 	if (bus != NULL)
17587c478bd9Sstevel@tonic-gate 		*bus = (int)PCI_REG_BUS_G(pci_rp->pci_phys_hi);
17597c478bd9Sstevel@tonic-gate 	if (device != NULL)
17607c478bd9Sstevel@tonic-gate 		*device = (int)PCI_REG_DEV_G(pci_rp->pci_phys_hi);
17617c478bd9Sstevel@tonic-gate 	if (func != NULL)
17627c478bd9Sstevel@tonic-gate 		*func = (int)PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
17637c478bd9Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
17647c478bd9Sstevel@tonic-gate 	return (0);
17657c478bd9Sstevel@tonic-gate }
17665cff7825Smh 
17675cff7825Smh /*
17685cff7825Smh  * Return the ACPI device node matching this dev_info node, if it
17695cff7825Smh  * exists in the ACPI tree.
17705cff7825Smh  */
17715cff7825Smh ACPI_STATUS
17725cff7825Smh acpica_get_handle(dev_info_t *dip, ACPI_HANDLE *rh)
17735cff7825Smh {
17745cff7825Smh 	ACPI_STATUS status;
17755cff7825Smh 	char *acpiname;
17765cff7825Smh 
17775cff7825Smh 	if (!d2a_done)
17785cff7825Smh 		scan_d2a_map();
17795cff7825Smh 
17805cff7825Smh 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
17815cff7825Smh 	    "acpi-namespace", &acpiname) != DDI_PROP_SUCCESS) {
17820e751525SEric Saxe 		return (AE_ERROR);
17835cff7825Smh 	}
17845cff7825Smh 
17855cff7825Smh 	status = AcpiGetHandle(NULL, acpiname, rh);
17865cff7825Smh 	ddi_prop_free((void *)acpiname);
17875cff7825Smh 	return (status);
17885cff7825Smh }
17895cff7825Smh 
17905cff7825Smh 
17915cff7825Smh 
17925cff7825Smh /*
17935cff7825Smh  * Manage OS data attachment to ACPI nodes
17945cff7825Smh  */
17955cff7825Smh 
17965cff7825Smh /*
17975cff7825Smh  * Return the (dev_info_t *) associated with the ACPI node.
17985cff7825Smh  */
17995cff7825Smh ACPI_STATUS
18005cff7825Smh acpica_get_devinfo(ACPI_HANDLE obj, dev_info_t **dipp)
18015cff7825Smh {
18025cff7825Smh 	ACPI_STATUS status;
18035cff7825Smh 	void *ptr;
18045cff7825Smh 
18055cff7825Smh 	status = AcpiGetData(obj, acpica_devinfo_handler, &ptr);
18065cff7825Smh 	if (status == AE_OK)
18075cff7825Smh 		*dipp = (dev_info_t *)ptr;
18085cff7825Smh 
18095cff7825Smh 	return (status);
18105cff7825Smh }
18115cff7825Smh 
18125cff7825Smh /*
18135cff7825Smh  * Set the dev_info_t associated with the ACPI node.
18145cff7825Smh  */
18155cff7825Smh static ACPI_STATUS
18165cff7825Smh acpica_set_devinfo(ACPI_HANDLE obj, dev_info_t *dip)
18175cff7825Smh {
18185cff7825Smh 	ACPI_STATUS status;
18195cff7825Smh 
18205cff7825Smh 	status = AcpiAttachData(obj, acpica_devinfo_handler, (void *)dip);
18215cff7825Smh 	return (status);
18225cff7825Smh }
18235cff7825Smh 
18245cff7825Smh /*
1825fa96bd91SMichael Corcoran  * Unset the dev_info_t associated with the ACPI node.
18265cff7825Smh  */
1827fa96bd91SMichael Corcoran static ACPI_STATUS
1828fa96bd91SMichael Corcoran acpica_unset_devinfo(ACPI_HANDLE obj)
18295cff7825Smh {
1830fa96bd91SMichael Corcoran 	return (AcpiDetachData(obj, acpica_devinfo_handler));
18315cff7825Smh }
18325cff7825Smh 
18335cff7825Smh /*
18345cff7825Smh  *
18355cff7825Smh  */
18365cff7825Smh void
1837fa96bd91SMichael Corcoran acpica_devinfo_handler(ACPI_HANDLE obj, UINT32 func, void *data)
18385cff7825Smh {
1839fa96bd91SMichael Corcoran 	/* noop */
18405cff7825Smh }
18415cff7825Smh 
1842fa96bd91SMichael Corcoran ACPI_STATUS
1843fa96bd91SMichael Corcoran acpica_build_processor_map(void)
18445cff7825Smh {
18455cff7825Smh 	ACPI_STATUS status;
18465cff7825Smh 	void *rv;
18475cff7825Smh 
18485cff7825Smh 	/*
18495cff7825Smh 	 * shouldn't be called more than once anyway
18505cff7825Smh 	 */
18515cff7825Smh 	if (cpu_map_built)
1852fa96bd91SMichael Corcoran 		return (AE_OK);
1853fa96bd91SMichael Corcoran 
1854fa96bd91SMichael Corcoran 	/*
1855fa96bd91SMichael Corcoran 	 * ACPI device configuration driver has built mapping information
1856fa96bd91SMichael Corcoran 	 * among processor id and object handle, no need to probe again.
1857fa96bd91SMichael Corcoran 	 */
1858fa96bd91SMichael Corcoran 	if (acpica_get_devcfg_feature(ACPI_DEVCFG_CPU)) {
1859fa96bd91SMichael Corcoran 		cpu_map_built = 1;
1860fa96bd91SMichael Corcoran 		return (AE_OK);
1861fa96bd91SMichael Corcoran 	}
18625cff7825Smh 
18635cff7825Smh 	/*
18645cff7825Smh 	 * Look for Processor objects
18655cff7825Smh 	 */
18665cff7825Smh 	status = AcpiWalkNamespace(ACPI_TYPE_PROCESSOR,
18675cff7825Smh 	    ACPI_ROOT_OBJECT,
18685cff7825Smh 	    4,
18695cff7825Smh 	    acpica_probe_processor,
18705cff7825Smh 	    NULL,
18715cff7825Smh 	    &rv);
18725cff7825Smh 	ASSERT(status == AE_OK);
18735cff7825Smh 
18745cff7825Smh 	/*
18755cff7825Smh 	 * Look for processor Device objects
18765cff7825Smh 	 */
18775cff7825Smh 	status = AcpiGetDevices("ACPI0007",
18785cff7825Smh 	    acpica_probe_processor,
18795cff7825Smh 	    NULL,
18805cff7825Smh 	    &rv);
18815cff7825Smh 	ASSERT(status == AE_OK);
18825cff7825Smh 	cpu_map_built = 1;
1883fa96bd91SMichael Corcoran 
1884fa96bd91SMichael Corcoran 	return (status);
1885fa96bd91SMichael Corcoran }
1886fa96bd91SMichael Corcoran 
1887fa96bd91SMichael Corcoran /*
1888fa96bd91SMichael Corcoran  * Grow cpu map table on demand.
1889fa96bd91SMichael Corcoran  */
1890fa96bd91SMichael Corcoran static void
1891fa96bd91SMichael Corcoran acpica_grow_cpu_map(void)
1892fa96bd91SMichael Corcoran {
1893fa96bd91SMichael Corcoran 	if (cpu_map_count == cpu_map_count_max) {
1894fa96bd91SMichael Corcoran 		size_t sz;
1895fa96bd91SMichael Corcoran 		struct cpu_map_item **new_map;
1896fa96bd91SMichael Corcoran 
1897fa96bd91SMichael Corcoran 		ASSERT(cpu_map_count_max < INT_MAX / 2);
1898fa96bd91SMichael Corcoran 		cpu_map_count_max += max_ncpus;
1899fa96bd91SMichael Corcoran 		new_map = kmem_zalloc(sizeof (cpu_map[0]) * cpu_map_count_max,
1900fa96bd91SMichael Corcoran 		    KM_SLEEP);
1901fa96bd91SMichael Corcoran 		if (cpu_map_count != 0) {
1902fa96bd91SMichael Corcoran 			ASSERT(cpu_map != NULL);
1903fa96bd91SMichael Corcoran 			sz = sizeof (cpu_map[0]) * cpu_map_count;
1904fa96bd91SMichael Corcoran 			kcopy(cpu_map, new_map, sz);
1905fa96bd91SMichael Corcoran 			kmem_free(cpu_map, sz);
1906fa96bd91SMichael Corcoran 		}
1907fa96bd91SMichael Corcoran 		cpu_map = new_map;
1908fa96bd91SMichael Corcoran 	}
1909fa96bd91SMichael Corcoran }
1910fa96bd91SMichael Corcoran 
1911fa96bd91SMichael Corcoran /*
1912fa96bd91SMichael Corcoran  * Maintain mapping information among (cpu id, ACPI processor id, APIC id,
1913fa96bd91SMichael Corcoran  * ACPI handle). The mapping table will be setup in two steps:
1914fa96bd91SMichael Corcoran  * 1) acpica_add_processor_to_map() builds mapping among APIC id, ACPI
1915fa96bd91SMichael Corcoran  *    processor id and ACPI object handle.
1916fa96bd91SMichael Corcoran  * 2) acpica_map_cpu() builds mapping among cpu id and ACPI processor id.
1917fa96bd91SMichael Corcoran  * On system with ACPI device configuration for CPU enabled, acpica_map_cpu()
1918fa96bd91SMichael Corcoran  * will be called before acpica_add_processor_to_map(), otherwise
1919fa96bd91SMichael Corcoran  * acpica_map_cpu() will be called after acpica_add_processor_to_map().
1920fa96bd91SMichael Corcoran  */
1921fa96bd91SMichael Corcoran ACPI_STATUS
1922fa96bd91SMichael Corcoran acpica_add_processor_to_map(UINT32 acpi_id, ACPI_HANDLE obj, UINT32 apic_id)
1923fa96bd91SMichael Corcoran {
1924fa96bd91SMichael Corcoran 	int i;
1925fa96bd91SMichael Corcoran 	ACPI_STATUS rc = AE_OK;
1926fa96bd91SMichael Corcoran 	struct cpu_map_item *item = NULL;
1927fa96bd91SMichael Corcoran 
1928fa96bd91SMichael Corcoran 	ASSERT(obj != NULL);
1929fa96bd91SMichael Corcoran 	if (obj == NULL) {
1930fa96bd91SMichael Corcoran 		return (AE_ERROR);
1931fa96bd91SMichael Corcoran 	}
1932fa96bd91SMichael Corcoran 
1933fa96bd91SMichael Corcoran 	mutex_enter(&cpu_map_lock);
1934fa96bd91SMichael Corcoran 
1935fa96bd91SMichael Corcoran 	/*
1936fa96bd91SMichael Corcoran 	 * Special case for uppc
1937fa96bd91SMichael Corcoran 	 * If we're a uppc system and ACPI device configuration for CPU has
1938fa96bd91SMichael Corcoran 	 * been disabled, there won't be a CPU map yet because uppc psm doesn't
1939fa96bd91SMichael Corcoran 	 * call acpica_map_cpu(). So create one and use the passed-in processor
1940fa96bd91SMichael Corcoran 	 * as CPU 0
1941fa96bd91SMichael Corcoran 	 */
1942fa96bd91SMichael Corcoran 	if (cpu_map == NULL &&
1943fa96bd91SMichael Corcoran 	    !acpica_get_devcfg_feature(ACPI_DEVCFG_CPU)) {
1944fa96bd91SMichael Corcoran 		acpica_grow_cpu_map();
1945fa96bd91SMichael Corcoran 		ASSERT(cpu_map != NULL);
1946fa96bd91SMichael Corcoran 		item = kmem_zalloc(sizeof (*item), KM_SLEEP);
1947fa96bd91SMichael Corcoran 		item->cpu_id = 0;
1948fa96bd91SMichael Corcoran 		item->proc_id = acpi_id;
1949fa96bd91SMichael Corcoran 		item->apic_id = apic_id;
1950fa96bd91SMichael Corcoran 		item->obj = obj;
1951fa96bd91SMichael Corcoran 		cpu_map[0] = item;
1952fa96bd91SMichael Corcoran 		cpu_map_count = 1;
1953fa96bd91SMichael Corcoran 		mutex_exit(&cpu_map_lock);
1954fa96bd91SMichael Corcoran 		return (AE_OK);
1955fa96bd91SMichael Corcoran 	}
1956fa96bd91SMichael Corcoran 
1957fa96bd91SMichael Corcoran 	for (i = 0; i < cpu_map_count; i++) {
1958fa96bd91SMichael Corcoran 		if (cpu_map[i]->obj == obj) {
1959fa96bd91SMichael Corcoran 			rc = AE_ALREADY_EXISTS;
1960fa96bd91SMichael Corcoran 			break;
1961fa96bd91SMichael Corcoran 		} else if (cpu_map[i]->proc_id == acpi_id) {
1962fa96bd91SMichael Corcoran 			ASSERT(item == NULL);
1963fa96bd91SMichael Corcoran 			item = cpu_map[i];
1964fa96bd91SMichael Corcoran 		}
1965fa96bd91SMichael Corcoran 	}
1966fa96bd91SMichael Corcoran 
1967fa96bd91SMichael Corcoran 	if (rc == AE_OK) {
1968fa96bd91SMichael Corcoran 		if (item != NULL) {
1969fa96bd91SMichael Corcoran 			/*
1970fa96bd91SMichael Corcoran 			 * ACPI alias objects may cause more than one objects
1971fa96bd91SMichael Corcoran 			 * with the same ACPI processor id, only remember the
1972fa96bd91SMichael Corcoran 			 * the first object encountered.
1973fa96bd91SMichael Corcoran 			 */
1974fa96bd91SMichael Corcoran 			if (item->obj == NULL) {
1975fa96bd91SMichael Corcoran 				item->obj = obj;
1976fa96bd91SMichael Corcoran 				item->apic_id = apic_id;
1977fa96bd91SMichael Corcoran 			} else {
1978fa96bd91SMichael Corcoran 				rc = AE_ALREADY_EXISTS;
1979fa96bd91SMichael Corcoran 			}
1980fa96bd91SMichael Corcoran 		} else if (cpu_map_count >= INT_MAX / 2) {
1981fa96bd91SMichael Corcoran 			rc = AE_NO_MEMORY;
1982fa96bd91SMichael Corcoran 		} else {
1983fa96bd91SMichael Corcoran 			acpica_grow_cpu_map();
1984fa96bd91SMichael Corcoran 			ASSERT(cpu_map != NULL);
1985fa96bd91SMichael Corcoran 			ASSERT(cpu_map_count < cpu_map_count_max);
1986fa96bd91SMichael Corcoran 			item = kmem_zalloc(sizeof (*item), KM_SLEEP);
1987fa96bd91SMichael Corcoran 			item->cpu_id = -1;
1988fa96bd91SMichael Corcoran 			item->proc_id = acpi_id;
1989fa96bd91SMichael Corcoran 			item->apic_id = apic_id;
1990fa96bd91SMichael Corcoran 			item->obj = obj;
1991fa96bd91SMichael Corcoran 			cpu_map[cpu_map_count] = item;
1992fa96bd91SMichael Corcoran 			cpu_map_count++;
1993fa96bd91SMichael Corcoran 		}
1994fa96bd91SMichael Corcoran 	}
1995fa96bd91SMichael Corcoran 
1996fa96bd91SMichael Corcoran 	mutex_exit(&cpu_map_lock);
1997fa96bd91SMichael Corcoran 
1998fa96bd91SMichael Corcoran 	return (rc);
1999fa96bd91SMichael Corcoran }
2000fa96bd91SMichael Corcoran 
2001fa96bd91SMichael Corcoran ACPI_STATUS
2002fa96bd91SMichael Corcoran acpica_remove_processor_from_map(UINT32 acpi_id)
2003fa96bd91SMichael Corcoran {
2004fa96bd91SMichael Corcoran 	int i;
2005fa96bd91SMichael Corcoran 	ACPI_STATUS rc = AE_NOT_EXIST;
2006fa96bd91SMichael Corcoran 
2007fa96bd91SMichael Corcoran 	mutex_enter(&cpu_map_lock);
2008fa96bd91SMichael Corcoran 	for (i = 0; i < cpu_map_count; i++) {
2009fa96bd91SMichael Corcoran 		if (cpu_map[i]->proc_id != acpi_id) {
2010fa96bd91SMichael Corcoran 			continue;
2011fa96bd91SMichael Corcoran 		}
2012fa96bd91SMichael Corcoran 		cpu_map[i]->obj = NULL;
2013fa96bd91SMichael Corcoran 		/* Free item if no more reference to it. */
2014fa96bd91SMichael Corcoran 		if (cpu_map[i]->cpu_id == -1) {
2015fa96bd91SMichael Corcoran 			kmem_free(cpu_map[i], sizeof (struct cpu_map_item));
2016fa96bd91SMichael Corcoran 			cpu_map[i] = NULL;
2017fa96bd91SMichael Corcoran 			cpu_map_count--;
2018fa96bd91SMichael Corcoran 			if (i != cpu_map_count) {
2019fa96bd91SMichael Corcoran 				cpu_map[i] = cpu_map[cpu_map_count];
2020fa96bd91SMichael Corcoran 				cpu_map[cpu_map_count] = NULL;
2021fa96bd91SMichael Corcoran 			}
2022fa96bd91SMichael Corcoran 		}
2023fa96bd91SMichael Corcoran 		rc = AE_OK;
2024fa96bd91SMichael Corcoran 		break;
2025fa96bd91SMichael Corcoran 	}
2026fa96bd91SMichael Corcoran 	mutex_exit(&cpu_map_lock);
2027fa96bd91SMichael Corcoran 
2028fa96bd91SMichael Corcoran 	return (rc);
2029fa96bd91SMichael Corcoran }
2030fa96bd91SMichael Corcoran 
2031fa96bd91SMichael Corcoran ACPI_STATUS
2032fa96bd91SMichael Corcoran acpica_map_cpu(processorid_t cpuid, UINT32 acpi_id)
2033fa96bd91SMichael Corcoran {
2034fa96bd91SMichael Corcoran 	int i;
2035fa96bd91SMichael Corcoran 	ACPI_STATUS rc = AE_OK;
2036fa96bd91SMichael Corcoran 	struct cpu_map_item *item = NULL;
2037fa96bd91SMichael Corcoran 
2038fa96bd91SMichael Corcoran 	ASSERT(cpuid != -1);
2039fa96bd91SMichael Corcoran 	if (cpuid == -1) {
2040fa96bd91SMichael Corcoran 		return (AE_ERROR);
2041fa96bd91SMichael Corcoran 	}
2042fa96bd91SMichael Corcoran 
2043fa96bd91SMichael Corcoran 	mutex_enter(&cpu_map_lock);
2044fa96bd91SMichael Corcoran 	for (i = 0; i < cpu_map_count; i++) {
2045fa96bd91SMichael Corcoran 		if (cpu_map[i]->cpu_id == cpuid) {
2046fa96bd91SMichael Corcoran 			rc = AE_ALREADY_EXISTS;
2047fa96bd91SMichael Corcoran 			break;
2048fa96bd91SMichael Corcoran 		} else if (cpu_map[i]->proc_id == acpi_id) {
2049fa96bd91SMichael Corcoran 			ASSERT(item == NULL);
2050fa96bd91SMichael Corcoran 			item = cpu_map[i];
2051fa96bd91SMichael Corcoran 		}
2052fa96bd91SMichael Corcoran 	}
2053fa96bd91SMichael Corcoran 	if (rc == AE_OK) {
2054fa96bd91SMichael Corcoran 		if (item != NULL) {
2055fa96bd91SMichael Corcoran 			if (item->cpu_id == -1) {
2056fa96bd91SMichael Corcoran 				item->cpu_id = cpuid;
2057fa96bd91SMichael Corcoran 			} else {
2058fa96bd91SMichael Corcoran 				rc = AE_ALREADY_EXISTS;
2059fa96bd91SMichael Corcoran 			}
2060fa96bd91SMichael Corcoran 		} else if (cpu_map_count >= INT_MAX / 2) {
2061fa96bd91SMichael Corcoran 			rc = AE_NO_MEMORY;
2062fa96bd91SMichael Corcoran 		} else {
2063fa96bd91SMichael Corcoran 			acpica_grow_cpu_map();
2064fa96bd91SMichael Corcoran 			ASSERT(cpu_map != NULL);
2065fa96bd91SMichael Corcoran 			ASSERT(cpu_map_count < cpu_map_count_max);
2066fa96bd91SMichael Corcoran 			item = kmem_zalloc(sizeof (*item), KM_SLEEP);
2067fa96bd91SMichael Corcoran 			item->cpu_id = cpuid;
2068fa96bd91SMichael Corcoran 			item->proc_id = acpi_id;
2069fa96bd91SMichael Corcoran 			item->apic_id = UINT32_MAX;
2070fa96bd91SMichael Corcoran 			item->obj = NULL;
2071fa96bd91SMichael Corcoran 			cpu_map[cpu_map_count] = item;
2072fa96bd91SMichael Corcoran 			cpu_map_count++;
2073fa96bd91SMichael Corcoran 		}
2074fa96bd91SMichael Corcoran 	}
2075fa96bd91SMichael Corcoran 	mutex_exit(&cpu_map_lock);
2076fa96bd91SMichael Corcoran 
2077fa96bd91SMichael Corcoran 	return (rc);
2078fa96bd91SMichael Corcoran }
2079fa96bd91SMichael Corcoran 
2080fa96bd91SMichael Corcoran ACPI_STATUS
2081fa96bd91SMichael Corcoran acpica_unmap_cpu(processorid_t cpuid)
2082fa96bd91SMichael Corcoran {
2083fa96bd91SMichael Corcoran 	int i;
2084fa96bd91SMichael Corcoran 	ACPI_STATUS rc = AE_NOT_EXIST;
2085fa96bd91SMichael Corcoran 
2086fa96bd91SMichael Corcoran 	ASSERT(cpuid != -1);
2087fa96bd91SMichael Corcoran 	if (cpuid == -1) {
2088fa96bd91SMichael Corcoran 		return (rc);
2089fa96bd91SMichael Corcoran 	}
2090fa96bd91SMichael Corcoran 
2091fa96bd91SMichael Corcoran 	mutex_enter(&cpu_map_lock);
2092fa96bd91SMichael Corcoran 	for (i = 0; i < cpu_map_count; i++) {
2093fa96bd91SMichael Corcoran 		if (cpu_map[i]->cpu_id != cpuid) {
2094fa96bd91SMichael Corcoran 			continue;
2095fa96bd91SMichael Corcoran 		}
2096fa96bd91SMichael Corcoran 		cpu_map[i]->cpu_id = -1;
2097fa96bd91SMichael Corcoran 		/* Free item if no more reference. */
2098fa96bd91SMichael Corcoran 		if (cpu_map[i]->obj == NULL) {
2099fa96bd91SMichael Corcoran 			kmem_free(cpu_map[i], sizeof (struct cpu_map_item));
2100fa96bd91SMichael Corcoran 			cpu_map[i] = NULL;
2101fa96bd91SMichael Corcoran 			cpu_map_count--;
2102fa96bd91SMichael Corcoran 			if (i != cpu_map_count) {
2103fa96bd91SMichael Corcoran 				cpu_map[i] = cpu_map[cpu_map_count];
2104fa96bd91SMichael Corcoran 				cpu_map[cpu_map_count] = NULL;
2105fa96bd91SMichael Corcoran 			}
2106fa96bd91SMichael Corcoran 		}
2107fa96bd91SMichael Corcoran 		rc = AE_OK;
2108fa96bd91SMichael Corcoran 		break;
2109fa96bd91SMichael Corcoran 	}
2110fa96bd91SMichael Corcoran 	mutex_exit(&cpu_map_lock);
2111fa96bd91SMichael Corcoran 
2112fa96bd91SMichael Corcoran 	return (rc);
2113fa96bd91SMichael Corcoran }
2114fa96bd91SMichael Corcoran 
2115fa96bd91SMichael Corcoran ACPI_STATUS
2116fa96bd91SMichael Corcoran acpica_get_cpu_object_by_cpuid(processorid_t cpuid, ACPI_HANDLE *hdlp)
2117fa96bd91SMichael Corcoran {
2118fa96bd91SMichael Corcoran 	int i;
2119fa96bd91SMichael Corcoran 	ACPI_STATUS rc = AE_NOT_EXIST;
2120fa96bd91SMichael Corcoran 
2121fa96bd91SMichael Corcoran 	ASSERT(cpuid != -1);
2122fa96bd91SMichael Corcoran 	if (cpuid == -1) {
2123fa96bd91SMichael Corcoran 		return (rc);
2124fa96bd91SMichael Corcoran 	}
2125fa96bd91SMichael Corcoran 
2126fa96bd91SMichael Corcoran 	mutex_enter(&cpu_map_lock);
2127fa96bd91SMichael Corcoran 	for (i = 0; i < cpu_map_count; i++) {
2128fa96bd91SMichael Corcoran 		if (cpu_map[i]->cpu_id == cpuid && cpu_map[i]->obj != NULL) {
2129fa96bd91SMichael Corcoran 			*hdlp = cpu_map[i]->obj;
2130fa96bd91SMichael Corcoran 			rc = AE_OK;
2131fa96bd91SMichael Corcoran 			break;
2132fa96bd91SMichael Corcoran 		}
2133fa96bd91SMichael Corcoran 	}
2134fa96bd91SMichael Corcoran 	mutex_exit(&cpu_map_lock);
2135fa96bd91SMichael Corcoran 
2136fa96bd91SMichael Corcoran 	return (rc);
2137fa96bd91SMichael Corcoran }
2138fa96bd91SMichael Corcoran 
2139fa96bd91SMichael Corcoran ACPI_STATUS
2140fa96bd91SMichael Corcoran acpica_get_cpu_object_by_procid(UINT32 procid, ACPI_HANDLE *hdlp)
2141fa96bd91SMichael Corcoran {
2142fa96bd91SMichael Corcoran 	int i;
2143fa96bd91SMichael Corcoran 	ACPI_STATUS rc = AE_NOT_EXIST;
2144fa96bd91SMichael Corcoran 
2145fa96bd91SMichael Corcoran 	mutex_enter(&cpu_map_lock);
2146fa96bd91SMichael Corcoran 	for (i = 0; i < cpu_map_count; i++) {
2147fa96bd91SMichael Corcoran 		if (cpu_map[i]->proc_id == procid && cpu_map[i]->obj != NULL) {
2148fa96bd91SMichael Corcoran 			*hdlp = cpu_map[i]->obj;
2149fa96bd91SMichael Corcoran 			rc = AE_OK;
2150fa96bd91SMichael Corcoran 			break;
2151fa96bd91SMichael Corcoran 		}
2152fa96bd91SMichael Corcoran 	}
2153fa96bd91SMichael Corcoran 	mutex_exit(&cpu_map_lock);
2154fa96bd91SMichael Corcoran 
2155fa96bd91SMichael Corcoran 	return (rc);
2156fa96bd91SMichael Corcoran }
2157fa96bd91SMichael Corcoran 
2158fa96bd91SMichael Corcoran ACPI_STATUS
2159fa96bd91SMichael Corcoran acpica_get_cpu_object_by_apicid(UINT32 apicid, ACPI_HANDLE *hdlp)
2160fa96bd91SMichael Corcoran {
2161fa96bd91SMichael Corcoran 	int i;
2162fa96bd91SMichael Corcoran 	ACPI_STATUS rc = AE_NOT_EXIST;
2163fa96bd91SMichael Corcoran 
2164fa96bd91SMichael Corcoran 	ASSERT(apicid != UINT32_MAX);
2165fa96bd91SMichael Corcoran 	if (apicid == UINT32_MAX) {
2166fa96bd91SMichael Corcoran 		return (rc);
2167fa96bd91SMichael Corcoran 	}
2168fa96bd91SMichael Corcoran 
2169fa96bd91SMichael Corcoran 	mutex_enter(&cpu_map_lock);
2170fa96bd91SMichael Corcoran 	for (i = 0; i < cpu_map_count; i++) {
2171fa96bd91SMichael Corcoran 		if (cpu_map[i]->apic_id == apicid && cpu_map[i]->obj != NULL) {
2172fa96bd91SMichael Corcoran 			*hdlp = cpu_map[i]->obj;
2173fa96bd91SMichael Corcoran 			rc = AE_OK;
2174fa96bd91SMichael Corcoran 			break;
2175fa96bd91SMichael Corcoran 		}
2176fa96bd91SMichael Corcoran 	}
2177fa96bd91SMichael Corcoran 	mutex_exit(&cpu_map_lock);
2178fa96bd91SMichael Corcoran 
2179fa96bd91SMichael Corcoran 	return (rc);
2180fa96bd91SMichael Corcoran }
2181fa96bd91SMichael Corcoran 
2182fa96bd91SMichael Corcoran void
2183fa96bd91SMichael Corcoran acpica_set_core_feature(uint64_t features)
2184fa96bd91SMichael Corcoran {
2185fa96bd91SMichael Corcoran 	atomic_or_64(&acpica_core_features, features);
2186fa96bd91SMichael Corcoran }
2187fa96bd91SMichael Corcoran 
2188fa96bd91SMichael Corcoran void
2189fa96bd91SMichael Corcoran acpica_clear_core_feature(uint64_t features)
2190fa96bd91SMichael Corcoran {
2191fa96bd91SMichael Corcoran 	atomic_and_64(&acpica_core_features, ~features);
2192fa96bd91SMichael Corcoran }
2193fa96bd91SMichael Corcoran 
2194fa96bd91SMichael Corcoran uint64_t
2195fa96bd91SMichael Corcoran acpica_get_core_feature(uint64_t features)
2196fa96bd91SMichael Corcoran {
2197fa96bd91SMichael Corcoran 	return (acpica_core_features & features);
2198fa96bd91SMichael Corcoran }
2199fa96bd91SMichael Corcoran 
2200fa96bd91SMichael Corcoran void
2201fa96bd91SMichael Corcoran acpica_set_devcfg_feature(uint64_t features)
2202fa96bd91SMichael Corcoran {
2203fa96bd91SMichael Corcoran 	atomic_or_64(&acpica_devcfg_features, features);
2204fa96bd91SMichael Corcoran }
2205fa96bd91SMichael Corcoran 
2206fa96bd91SMichael Corcoran void
2207fa96bd91SMichael Corcoran acpica_clear_devcfg_feature(uint64_t features)
2208fa96bd91SMichael Corcoran {
2209fa96bd91SMichael Corcoran 	atomic_and_64(&acpica_devcfg_features, ~features);
2210fa96bd91SMichael Corcoran }
2211fa96bd91SMichael Corcoran 
2212fa96bd91SMichael Corcoran uint64_t
2213fa96bd91SMichael Corcoran acpica_get_devcfg_feature(uint64_t features)
2214fa96bd91SMichael Corcoran {
2215fa96bd91SMichael Corcoran 	return (acpica_devcfg_features & features);
22165cff7825Smh }
22170e751525SEric Saxe 
22180e751525SEric Saxe void
22190e751525SEric Saxe acpica_get_global_FADT(ACPI_TABLE_FADT **gbl_FADT)
22200e751525SEric Saxe {
22210e751525SEric Saxe 	*gbl_FADT = &AcpiGbl_FADT;
22220e751525SEric Saxe }
2223*78d5422cSMark Haywood 
2224*78d5422cSMark Haywood void
2225*78d5422cSMark Haywood acpica_write_cpupm_capabilities(boolean_t pstates, boolean_t cstates)
2226*78d5422cSMark Haywood {
2227*78d5422cSMark Haywood 	if (pstates && AcpiGbl_FADT.PstateControl != 0)
2228*78d5422cSMark Haywood 		(void) AcpiHwRegisterWrite(ACPI_REGISTER_SMI_COMMAND_BLOCK,
2229*78d5422cSMark Haywood 		    AcpiGbl_FADT.PstateControl);
2230*78d5422cSMark Haywood 
2231*78d5422cSMark Haywood 	if (cstates && AcpiGbl_FADT.CstControl != 0)
2232*78d5422cSMark Haywood 		(void) AcpiHwRegisterWrite(ACPI_REGISTER_SMI_COMMAND_BLOCK,
2233*78d5422cSMark Haywood 		    AcpiGbl_FADT.CstControl);
2234*78d5422cSMark Haywood }
2235