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.
2535786f68SRobert Mustacchi * Copyright 2018 Joyent, Inc.
26*3349e36eSPaul Winder * Copyright 2019 Western Digital Corporation
277c478bd9Sstevel@tonic-gate */
28fa96bd91SMichael Corcoran /*
29a3114836SGerry Liu * Copyright (c) 2009-2010, Intel Corporation.
30fa96bd91SMichael Corcoran * All rights reserved.
31fa96bd91SMichael Corcoran */
328e56767dSsmall /*
338e56767dSsmall * ACPI CA OSL for Solaris x86
348e56767dSsmall */
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/psm.h>
397c478bd9Sstevel@tonic-gate #include <sys/pci_cfgspace.h>
40b9bfdccdSStuart Maybee #include <sys/apic.h>
417c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
424b1ce0a9SDana Myers #include <sys/sunddi.h>
435cff7825Smh #include <sys/sunndi.h>
447c478bd9Sstevel@tonic-gate #include <sys/pci.h>
457c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
467c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
477c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
48b72d5b75SMichael Corcoran #include <sys/x86_archext.h>
497c478bd9Sstevel@tonic-gate #include <sys/note.h>
509bb29364SDana Myers #include <sys/promif.h>
517c478bd9Sstevel@tonic-gate
5278d5422cSMark Haywood #include <sys/acpi/accommon.h>
537c478bd9Sstevel@tonic-gate #include <sys/acpica.h>
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #define MAX_DAT_FILE_SIZE (64*1024)
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate /* local functions */
587c478bd9Sstevel@tonic-gate static int CompressEisaID(char *np);
597c478bd9Sstevel@tonic-gate
605cff7825Smh static void scan_d2a_subtree(dev_info_t *dip, ACPI_HANDLE acpiobj, int bus);
617c478bd9Sstevel@tonic-gate static int acpica_query_bbn_problem(void);
627c478bd9Sstevel@tonic-gate static int acpica_find_pcibus(int busno, ACPI_HANDLE *rh);
637c478bd9Sstevel@tonic-gate static int acpica_eval_hid(ACPI_HANDLE dev, char *method, int *rint);
645cff7825Smh static ACPI_STATUS acpica_set_devinfo(ACPI_HANDLE, dev_info_t *);
65fa96bd91SMichael Corcoran static ACPI_STATUS acpica_unset_devinfo(ACPI_HANDLE);
6657190917SDana Myers static void acpica_devinfo_handler(ACPI_HANDLE, void *);
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate * Event queue vars
707c478bd9Sstevel@tonic-gate */
717c478bd9Sstevel@tonic-gate int acpica_eventq_init = 0;
7227f7c583Smyers ddi_taskq_t *osl_eventq[OSL_EC_BURST_HANDLER+1];
737c478bd9Sstevel@tonic-gate
74db2bae30SDana Myers /*
75db2bae30SDana Myers * Priorities relative to minclsyspri that each taskq
76db2bae30SDana Myers * run at; OSL_NOTIFY_HANDLER needs to run at a higher
77db2bae30SDana Myers * priority than OSL_GPE_HANDLER. There's an implicit
78db2bae30SDana Myers * assumption that no priority here results in exceeding
79db2bae30SDana Myers * maxclsyspri.
80db2bae30SDana Myers * Note: these initializations need to match the order of
81db2bae30SDana Myers * ACPI_EXECUTE_TYPE.
82db2bae30SDana Myers */
83db2bae30SDana Myers int osl_eventq_pri_delta[OSL_EC_BURST_HANDLER+1] = {
84db2bae30SDana Myers 0, /* OSL_GLOBAL_LOCK_HANDLER */
85db2bae30SDana Myers 2, /* OSL_NOTIFY_HANDLER */
86db2bae30SDana Myers 0, /* OSL_GPE_HANDLER */
87db2bae30SDana Myers 0, /* OSL_DEBUGGER_THREAD */
88db2bae30SDana Myers 0, /* OSL_EC_POLL_HANDLER */
89db2bae30SDana Myers 0 /* OSL_EC_BURST_HANDLER */
90db2bae30SDana Myers };
91db2bae30SDana Myers
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate * Note, if you change this path, you need to update
947c478bd9Sstevel@tonic-gate * /boot/grub/filelist.ramdisk and pkg SUNWckr/prototype_i386
957c478bd9Sstevel@tonic-gate */
967c478bd9Sstevel@tonic-gate static char *acpi_table_path = "/boot/acpi/tables/";
977c478bd9Sstevel@tonic-gate
985cff7825Smh /* non-zero while scan_d2a_map() is working */
995cff7825Smh static int scanning_d2a_map = 0;
1005cff7825Smh static int d2a_done = 0;
1017c478bd9Sstevel@tonic-gate
102fa96bd91SMichael Corcoran /* features supported by ACPICA and ACPI device configuration. */
103a3114836SGerry Liu uint64_t acpica_core_features = ACPI_FEATURE_OSI_MODULE;
104fa96bd91SMichael Corcoran static uint64_t acpica_devcfg_features = 0;
105fa96bd91SMichael Corcoran
106137e2f25SGuoli Shu /* set by acpi_poweroff() in PSMs and appm_ioctl() in acpippm for S3 */
107137e2f25SGuoli Shu int acpica_use_safe_delay = 0;
108859dcd3dSmyers
1095cff7825Smh /* CPU mapping data */
1105cff7825Smh struct cpu_map_item {
111fa96bd91SMichael Corcoran processorid_t cpu_id;
112b6917abeSmishra UINT32 proc_id;
113fa96bd91SMichael Corcoran UINT32 apic_id;
1145cff7825Smh ACPI_HANDLE obj;
1155cff7825Smh };
1165cff7825Smh
117a3114836SGerry Liu kmutex_t cpu_map_lock;
1185cff7825Smh static struct cpu_map_item **cpu_map = NULL;
119fa96bd91SMichael Corcoran static int cpu_map_count_max = 0;
1205cff7825Smh static int cpu_map_count = 0;
1215cff7825Smh static int cpu_map_built = 0;
1225cff7825Smh
123b72d5b75SMichael Corcoran /*
124b72d5b75SMichael Corcoran * On systems with the uppc PSM only, acpica_map_cpu() won't be called at all.
125b72d5b75SMichael Corcoran * This flag is used to check for uppc-only systems by detecting whether
126b72d5b75SMichael Corcoran * acpica_map_cpu() has been called or not.
127b72d5b75SMichael Corcoran */
128b72d5b75SMichael Corcoran static int cpu_map_called = 0;
129b72d5b75SMichael Corcoran
1305cff7825Smh static int acpi_has_broken_bbn = -1;
1315cff7825Smh
13287bb58d6Smyers /* buffer for AcpiOsVprintf() */
13387bb58d6Smyers #define ACPI_OSL_PR_BUFLEN 1024
13487bb58d6Smyers static char *acpi_osl_pr_buffer = NULL;
13587bb58d6Smyers static int acpi_osl_pr_buflen;
13687bb58d6Smyers
1375cff7825Smh #define D2A_DEBUG
1385cff7825Smh
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate *
1417c478bd9Sstevel@tonic-gate */
1427c478bd9Sstevel@tonic-gate static void
discard_event_queues()1437c478bd9Sstevel@tonic-gate discard_event_queues()
1447c478bd9Sstevel@tonic-gate {
14527f7c583Smyers int i;
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate * destroy event queues
1497c478bd9Sstevel@tonic-gate */
15027f7c583Smyers for (i = OSL_GLOBAL_LOCK_HANDLER; i <= OSL_EC_BURST_HANDLER; i++) {
15127f7c583Smyers if (osl_eventq[i])
15227f7c583Smyers ddi_taskq_destroy(osl_eventq[i]);
15327f7c583Smyers }
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate *
1597c478bd9Sstevel@tonic-gate */
1607c478bd9Sstevel@tonic-gate static ACPI_STATUS
init_event_queues()1617c478bd9Sstevel@tonic-gate init_event_queues()
1627c478bd9Sstevel@tonic-gate {
16327f7c583Smyers char namebuf[32];
16427f7c583Smyers int i, error = 0;
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate * Initialize event queues
1687c478bd9Sstevel@tonic-gate */
1697c478bd9Sstevel@tonic-gate
170db2bae30SDana Myers /* Always allocate only 1 thread per queue to force FIFO execution */
17127f7c583Smyers for (i = OSL_GLOBAL_LOCK_HANDLER; i <= OSL_EC_BURST_HANDLER; i++) {
17227f7c583Smyers snprintf(namebuf, 32, "ACPI%d", i);
173db2bae30SDana Myers osl_eventq[i] = ddi_taskq_create(NULL, namebuf, 1,
174db2bae30SDana Myers osl_eventq_pri_delta[i] + minclsyspri, 0);
17527f7c583Smyers if (osl_eventq[i] == NULL)
17627f7c583Smyers error++;
17727f7c583Smyers }
17827f7c583Smyers
17927f7c583Smyers if (error != 0) {
1807c478bd9Sstevel@tonic-gate discard_event_queues();
1817c478bd9Sstevel@tonic-gate #ifdef DEBUG
1827c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "!acpica: could not initialize event queues");
1837c478bd9Sstevel@tonic-gate #endif
1847c478bd9Sstevel@tonic-gate return (AE_ERROR);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate acpica_eventq_init = 1;
1887c478bd9Sstevel@tonic-gate return (AE_OK);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate /*
19287bb58d6Smyers * One-time initialization of OSL layer
1937c478bd9Sstevel@tonic-gate */
1947c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsInitialize(void)1957c478bd9Sstevel@tonic-gate AcpiOsInitialize(void)
1967c478bd9Sstevel@tonic-gate {
19787bb58d6Smyers /*
19887bb58d6Smyers * Allocate buffer for AcpiOsVprintf() here to avoid
19987bb58d6Smyers * kmem_alloc()/kmem_free() at high PIL
20087bb58d6Smyers */
20187bb58d6Smyers acpi_osl_pr_buffer = kmem_alloc(ACPI_OSL_PR_BUFLEN, KM_SLEEP);
20287bb58d6Smyers if (acpi_osl_pr_buffer != NULL)
20387bb58d6Smyers acpi_osl_pr_buflen = ACPI_OSL_PR_BUFLEN;
20487bb58d6Smyers
2057c478bd9Sstevel@tonic-gate return (AE_OK);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate /*
20987bb58d6Smyers * One-time shut-down of OSL layer
2107c478bd9Sstevel@tonic-gate */
2117c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsTerminate(void)2127c478bd9Sstevel@tonic-gate AcpiOsTerminate(void)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate
21587bb58d6Smyers if (acpi_osl_pr_buffer != NULL)
21687bb58d6Smyers kmem_free(acpi_osl_pr_buffer, acpi_osl_pr_buflen);
21787bb58d6Smyers
2187c478bd9Sstevel@tonic-gate discard_event_queues();
2197c478bd9Sstevel@tonic-gate return (AE_OK);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate
223db2bae30SDana Myers ACPI_PHYSICAL_ADDRESS
AcpiOsGetRootPointer()224db2bae30SDana Myers AcpiOsGetRootPointer()
2257c478bd9Sstevel@tonic-gate {
226db2bae30SDana Myers ACPI_PHYSICAL_ADDRESS Address;
227f2be5148Sszhou
228f2be5148Sszhou /*
229f2be5148Sszhou * For EFI firmware, the root pointer is defined in EFI systab.
230f2be5148Sszhou * The boot code process the table and put the physical address
231f2be5148Sszhou * in the acpi-root-tab property.
232f2be5148Sszhou */
2331738dd6eSToomas Soome Address = ddi_prop_get_int64(DDI_DEV_T_ANY, ddi_root_node(),
234ee441788SToomas Soome DDI_PROP_DONTPASS, "acpi-root-tab", 0);
235db2bae30SDana Myers
236ee441788SToomas Soome if ((Address == 0) && ACPI_FAILURE(AcpiFindRootPointer(&Address)))
237ee441788SToomas Soome Address = 0;
238db2bae30SDana Myers
239db2bae30SDana Myers return (Address);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2437c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsPredefinedOverride(const ACPI_PREDEFINED_NAMES * InitVal,ACPI_STRING * NewVal)2447c478bd9Sstevel@tonic-gate AcpiOsPredefinedOverride(const ACPI_PREDEFINED_NAMES *InitVal,
2451738dd6eSToomas Soome ACPI_STRING *NewVal)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate *NewVal = 0;
2497c478bd9Sstevel@tonic-gate return (AE_OK);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate static void
acpica_strncpy(char * dest,const char * src,int len)2537c478bd9Sstevel@tonic-gate acpica_strncpy(char *dest, const char *src, int len)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate /*LINTED*/
2577c478bd9Sstevel@tonic-gate while ((*dest++ = *src++) && (--len > 0))
2587c478bd9Sstevel@tonic-gate /* copy the string */;
2597c478bd9Sstevel@tonic-gate *dest = '\0';
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsTableOverride(ACPI_TABLE_HEADER * ExistingTable,ACPI_TABLE_HEADER ** NewTable)2637c478bd9Sstevel@tonic-gate AcpiOsTableOverride(ACPI_TABLE_HEADER *ExistingTable,
2641738dd6eSToomas Soome ACPI_TABLE_HEADER **NewTable)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate char signature[5];
2677c478bd9Sstevel@tonic-gate char oemid[7];
2687c478bd9Sstevel@tonic-gate char oemtableid[9];
2697c478bd9Sstevel@tonic-gate struct _buf *file;
2707c478bd9Sstevel@tonic-gate char *buf1, *buf2;
2717c478bd9Sstevel@tonic-gate int count;
2727c478bd9Sstevel@tonic-gate char acpi_table_loc[128];
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate acpica_strncpy(signature, ExistingTable->Signature, 4);
2757c478bd9Sstevel@tonic-gate acpica_strncpy(oemid, ExistingTable->OemId, 6);
2767c478bd9Sstevel@tonic-gate acpica_strncpy(oemtableid, ExistingTable->OemTableId, 8);
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate /* File name format is "signature_oemid_oemtableid.dat" */
2797c478bd9Sstevel@tonic-gate (void) strcpy(acpi_table_loc, acpi_table_path);
2807c478bd9Sstevel@tonic-gate (void) strcat(acpi_table_loc, signature); /* for example, DSDT */
2817c478bd9Sstevel@tonic-gate (void) strcat(acpi_table_loc, "_");
2827c478bd9Sstevel@tonic-gate (void) strcat(acpi_table_loc, oemid); /* for example, IntelR */
2837c478bd9Sstevel@tonic-gate (void) strcat(acpi_table_loc, "_");
2847c478bd9Sstevel@tonic-gate (void) strcat(acpi_table_loc, oemtableid); /* for example, AWRDACPI */
2857c478bd9Sstevel@tonic-gate (void) strcat(acpi_table_loc, ".dat");
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate file = kobj_open_file(acpi_table_loc);
2887c478bd9Sstevel@tonic-gate if (file == (struct _buf *)-1) {
2897c478bd9Sstevel@tonic-gate *NewTable = 0;
2907c478bd9Sstevel@tonic-gate return (AE_OK);
2917c478bd9Sstevel@tonic-gate } else {
2927c478bd9Sstevel@tonic-gate buf1 = (char *)kmem_alloc(MAX_DAT_FILE_SIZE, KM_SLEEP);
2937c478bd9Sstevel@tonic-gate count = kobj_read_file(file, buf1, MAX_DAT_FILE_SIZE-1, 0);
2947c478bd9Sstevel@tonic-gate if (count >= MAX_DAT_FILE_SIZE) {
2957c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "!acpica: table %s file size too big",
2967c478bd9Sstevel@tonic-gate acpi_table_loc);
2977c478bd9Sstevel@tonic-gate *NewTable = 0;
2987c478bd9Sstevel@tonic-gate } else {
2997c478bd9Sstevel@tonic-gate buf2 = (char *)kmem_alloc(count, KM_SLEEP);
3007c478bd9Sstevel@tonic-gate (void) memcpy(buf2, buf1, count);
3017c478bd9Sstevel@tonic-gate *NewTable = (ACPI_TABLE_HEADER *)buf2;
3027c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "!acpica: replacing table: %s",
3037c478bd9Sstevel@tonic-gate acpi_table_loc);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate kobj_close_file(file);
3077c478bd9Sstevel@tonic-gate kmem_free(buf1, MAX_DAT_FILE_SIZE);
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate return (AE_OK);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate
3127b1019a6SJerry Jelinek ACPI_STATUS
AcpiOsPhysicalTableOverride(ACPI_TABLE_HEADER * ExistingTable,ACPI_PHYSICAL_ADDRESS * NewAddress,UINT32 * NewTableLength)3137b1019a6SJerry Jelinek AcpiOsPhysicalTableOverride(ACPI_TABLE_HEADER *ExistingTable,
3147b1019a6SJerry Jelinek ACPI_PHYSICAL_ADDRESS *NewAddress, UINT32 *NewTableLength)
3157b1019a6SJerry Jelinek {
3167b1019a6SJerry Jelinek return (AE_SUPPORT);
3177b1019a6SJerry Jelinek }
3187c478bd9Sstevel@tonic-gate
31930082d0cSmyers /*
32030082d0cSmyers * ACPI semaphore implementation
32130082d0cSmyers */
32230082d0cSmyers typedef struct {
32330082d0cSmyers kmutex_t mutex;
32430082d0cSmyers kcondvar_t cv;
32530082d0cSmyers uint32_t available;
32630082d0cSmyers uint32_t initial;
32730082d0cSmyers uint32_t maximum;
32830082d0cSmyers } acpi_sema_t;
32930082d0cSmyers
33030082d0cSmyers /*
33130082d0cSmyers *
33230082d0cSmyers */
33330082d0cSmyers void
acpi_sema_init(acpi_sema_t * sp,unsigned max,unsigned count)33427f7c583Smyers acpi_sema_init(acpi_sema_t *sp, unsigned max, unsigned count)
33530082d0cSmyers {
33630082d0cSmyers mutex_init(&sp->mutex, NULL, MUTEX_DRIVER, NULL);
33730082d0cSmyers cv_init(&sp->cv, NULL, CV_DRIVER, NULL);
33830082d0cSmyers /* no need to enter mutex here at creation */
33930082d0cSmyers sp->available = count;
34030082d0cSmyers sp->initial = count;
34130082d0cSmyers sp->maximum = max;
34230082d0cSmyers }
34330082d0cSmyers
34430082d0cSmyers /*
34530082d0cSmyers *
34630082d0cSmyers */
34730082d0cSmyers void
acpi_sema_destroy(acpi_sema_t * sp)34830082d0cSmyers acpi_sema_destroy(acpi_sema_t *sp)
34930082d0cSmyers {
35030082d0cSmyers
35130082d0cSmyers cv_destroy(&sp->cv);
35230082d0cSmyers mutex_destroy(&sp->mutex);
35330082d0cSmyers }
35430082d0cSmyers
35530082d0cSmyers /*
35630082d0cSmyers *
35730082d0cSmyers */
35830082d0cSmyers ACPI_STATUS
acpi_sema_p(acpi_sema_t * sp,unsigned count,uint16_t wait_time)35930082d0cSmyers acpi_sema_p(acpi_sema_t *sp, unsigned count, uint16_t wait_time)
36030082d0cSmyers {
36130082d0cSmyers ACPI_STATUS rv = AE_OK;
36230082d0cSmyers clock_t deadline;
36330082d0cSmyers
36430082d0cSmyers mutex_enter(&sp->mutex);
36530082d0cSmyers
36630082d0cSmyers if (sp->available >= count) {
36730082d0cSmyers /*
36830082d0cSmyers * Enough units available, no blocking
36930082d0cSmyers */
37030082d0cSmyers sp->available -= count;
37130082d0cSmyers mutex_exit(&sp->mutex);
37230082d0cSmyers return (rv);
37330082d0cSmyers } else if (wait_time == 0) {
37430082d0cSmyers /*
37530082d0cSmyers * Not enough units available and timeout
37630082d0cSmyers * specifies no blocking
37730082d0cSmyers */
37830082d0cSmyers rv = AE_TIME;
37930082d0cSmyers mutex_exit(&sp->mutex);
38030082d0cSmyers return (rv);
38130082d0cSmyers }
38230082d0cSmyers
38330082d0cSmyers /*
38430082d0cSmyers * Not enough units available and timeout specifies waiting
38530082d0cSmyers */
38630082d0cSmyers if (wait_time != ACPI_WAIT_FOREVER)
38730082d0cSmyers deadline = ddi_get_lbolt() +
38830082d0cSmyers (clock_t)drv_usectohz(wait_time * 1000);
38930082d0cSmyers
39030082d0cSmyers do {
39130082d0cSmyers if (wait_time == ACPI_WAIT_FOREVER)
39230082d0cSmyers cv_wait(&sp->cv, &sp->mutex);
39330082d0cSmyers else if (cv_timedwait(&sp->cv, &sp->mutex, deadline) < 0) {
39430082d0cSmyers rv = AE_TIME;
39530082d0cSmyers break;
39630082d0cSmyers }
39730082d0cSmyers } while (sp->available < count);
39830082d0cSmyers
39930082d0cSmyers /* if we dropped out of the wait with AE_OK, we got the units */
40030082d0cSmyers if (rv == AE_OK)
40130082d0cSmyers sp->available -= count;
40230082d0cSmyers
40330082d0cSmyers mutex_exit(&sp->mutex);
40430082d0cSmyers return (rv);
40530082d0cSmyers }
40630082d0cSmyers
40730082d0cSmyers /*
40830082d0cSmyers *
40930082d0cSmyers */
41030082d0cSmyers void
acpi_sema_v(acpi_sema_t * sp,unsigned count)41130082d0cSmyers acpi_sema_v(acpi_sema_t *sp, unsigned count)
41230082d0cSmyers {
41330082d0cSmyers mutex_enter(&sp->mutex);
41430082d0cSmyers sp->available += count;
41530082d0cSmyers cv_broadcast(&sp->cv);
41630082d0cSmyers mutex_exit(&sp->mutex);
41730082d0cSmyers }
41830082d0cSmyers
41930082d0cSmyers
4207c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsCreateSemaphore(UINT32 MaxUnits,UINT32 InitialUnits,ACPI_HANDLE * OutHandle)4217c478bd9Sstevel@tonic-gate AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits,
4221738dd6eSToomas Soome ACPI_HANDLE *OutHandle)
4237c478bd9Sstevel@tonic-gate {
42430082d0cSmyers acpi_sema_t *sp;
4257c478bd9Sstevel@tonic-gate
4267c478bd9Sstevel@tonic-gate if ((OutHandle == NULL) || (InitialUnits > MaxUnits))
4277c478bd9Sstevel@tonic-gate return (AE_BAD_PARAMETER);
4287c478bd9Sstevel@tonic-gate
42930082d0cSmyers sp = (acpi_sema_t *)kmem_alloc(sizeof (acpi_sema_t), KM_SLEEP);
43027f7c583Smyers acpi_sema_init(sp, MaxUnits, InitialUnits);
43130082d0cSmyers *OutHandle = (ACPI_HANDLE)sp;
4327c478bd9Sstevel@tonic-gate return (AE_OK);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsDeleteSemaphore(ACPI_HANDLE Handle)4377c478bd9Sstevel@tonic-gate AcpiOsDeleteSemaphore(ACPI_HANDLE Handle)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate
44030082d0cSmyers if (Handle == NULL)
44130082d0cSmyers return (AE_BAD_PARAMETER);
44230082d0cSmyers
44330082d0cSmyers acpi_sema_destroy((acpi_sema_t *)Handle);
44430082d0cSmyers kmem_free((void *)Handle, sizeof (acpi_sema_t));
4457c478bd9Sstevel@tonic-gate return (AE_OK);
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate
4487c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsWaitSemaphore(ACPI_HANDLE Handle,UINT32 Units,UINT16 Timeout)4497c478bd9Sstevel@tonic-gate AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT16 Timeout)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate
45230082d0cSmyers if ((Handle == NULL) || (Units < 1))
45330082d0cSmyers return (AE_BAD_PARAMETER);
4547c478bd9Sstevel@tonic-gate
45530082d0cSmyers return (acpi_sema_p((acpi_sema_t *)Handle, Units, Timeout));
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsSignalSemaphore(ACPI_HANDLE Handle,UINT32 Units)4597c478bd9Sstevel@tonic-gate AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate
46230082d0cSmyers if ((Handle == NULL) || (Units < 1))
46330082d0cSmyers return (AE_BAD_PARAMETER);
46430082d0cSmyers
46530082d0cSmyers acpi_sema_v((acpi_sema_t *)Handle, Units);
4667c478bd9Sstevel@tonic-gate return (AE_OK);
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate
4697c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsCreateLock(ACPI_HANDLE * OutHandle)4707c478bd9Sstevel@tonic-gate AcpiOsCreateLock(ACPI_HANDLE *OutHandle)
4717c478bd9Sstevel@tonic-gate {
47230082d0cSmyers kmutex_t *mp;
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate if (OutHandle == NULL)
4757c478bd9Sstevel@tonic-gate return (AE_BAD_PARAMETER);
4767c478bd9Sstevel@tonic-gate
47730082d0cSmyers mp = (kmutex_t *)kmem_alloc(sizeof (kmutex_t), KM_SLEEP);
47830082d0cSmyers mutex_init(mp, NULL, MUTEX_DRIVER, NULL);
47930082d0cSmyers *OutHandle = (ACPI_HANDLE)mp;
4807c478bd9Sstevel@tonic-gate return (AE_OK);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate
4837c478bd9Sstevel@tonic-gate void
AcpiOsDeleteLock(ACPI_HANDLE Handle)4847c478bd9Sstevel@tonic-gate AcpiOsDeleteLock(ACPI_HANDLE Handle)
4857c478bd9Sstevel@tonic-gate {
48630082d0cSmyers
48730082d0cSmyers if (Handle == NULL)
48830082d0cSmyers return;
48930082d0cSmyers
49030082d0cSmyers mutex_destroy((kmutex_t *)Handle);
49130082d0cSmyers kmem_free((void *)Handle, sizeof (kmutex_t));
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate
494db2bae30SDana Myers ACPI_CPU_FLAGS
AcpiOsAcquireLock(ACPI_HANDLE Handle)495450d6964Smyers AcpiOsAcquireLock(ACPI_HANDLE Handle)
4967c478bd9Sstevel@tonic-gate {
49730082d0cSmyers
4980e751525SEric Saxe
4990e751525SEric Saxe if (Handle == NULL)
5000e751525SEric Saxe return (AE_BAD_PARAMETER);
5010e751525SEric Saxe
5020e751525SEric Saxe if (curthread == CPU->cpu_idle_thread) {
5030e751525SEric Saxe while (!mutex_tryenter((kmutex_t *)Handle))
5040e751525SEric Saxe /* spin */;
5050e751525SEric Saxe } else
5060e751525SEric Saxe mutex_enter((kmutex_t *)Handle);
5070e751525SEric Saxe return (AE_OK);
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate
5107c478bd9Sstevel@tonic-gate void
AcpiOsReleaseLock(ACPI_HANDLE Handle,ACPI_CPU_FLAGS Flags)511db2bae30SDana Myers AcpiOsReleaseLock(ACPI_HANDLE Handle, ACPI_CPU_FLAGS Flags)
5127c478bd9Sstevel@tonic-gate {
513450d6964Smyers _NOTE(ARGUNUSED(Flags))
5147c478bd9Sstevel@tonic-gate
51530082d0cSmyers mutex_exit((kmutex_t *)Handle);
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate
5187c478bd9Sstevel@tonic-gate
5197c478bd9Sstevel@tonic-gate void *
AcpiOsAllocate(ACPI_SIZE Size)5207c478bd9Sstevel@tonic-gate AcpiOsAllocate(ACPI_SIZE Size)
5217c478bd9Sstevel@tonic-gate {
5227c478bd9Sstevel@tonic-gate ACPI_SIZE *tmp_ptr;
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate Size += sizeof (Size);
5257c478bd9Sstevel@tonic-gate tmp_ptr = (ACPI_SIZE *)kmem_zalloc(Size, KM_SLEEP);
5267c478bd9Sstevel@tonic-gate *tmp_ptr++ = Size;
52730082d0cSmyers return (tmp_ptr);
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate
5307c478bd9Sstevel@tonic-gate void
AcpiOsFree(void * Memory)5317c478bd9Sstevel@tonic-gate AcpiOsFree(void *Memory)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate ACPI_SIZE size, *tmp_ptr;
5347c478bd9Sstevel@tonic-gate
5357c478bd9Sstevel@tonic-gate tmp_ptr = (ACPI_SIZE *)Memory;
5367c478bd9Sstevel@tonic-gate tmp_ptr -= 1;
5377c478bd9Sstevel@tonic-gate size = *tmp_ptr;
53830082d0cSmyers kmem_free(tmp_ptr, size);
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate
541b9bfdccdSStuart Maybee static int napics_found; /* number of ioapic addresses in array */
542b9bfdccdSStuart Maybee static ACPI_PHYSICAL_ADDRESS ioapic_paddr[MAX_IO_APIC];
543b9bfdccdSStuart Maybee static ACPI_TABLE_MADT *acpi_mapic_dtp = NULL;
544b9bfdccdSStuart Maybee static void *dummy_ioapicadr;
545b9bfdccdSStuart Maybee
546b9bfdccdSStuart Maybee void
acpica_find_ioapics(void)547b9bfdccdSStuart Maybee acpica_find_ioapics(void)
548b9bfdccdSStuart Maybee {
549b9bfdccdSStuart Maybee int madt_seen, madt_size;
550b9bfdccdSStuart Maybee ACPI_SUBTABLE_HEADER *ap;
551b9bfdccdSStuart Maybee ACPI_MADT_IO_APIC *mia;
552b9bfdccdSStuart Maybee
553b9bfdccdSStuart Maybee if (acpi_mapic_dtp != NULL)
554b9bfdccdSStuart Maybee return; /* already parsed table */
555b9bfdccdSStuart Maybee if (AcpiGetTable(ACPI_SIG_MADT, 1,
556b9bfdccdSStuart Maybee (ACPI_TABLE_HEADER **) &acpi_mapic_dtp) != AE_OK)
557b9bfdccdSStuart Maybee return;
558b9bfdccdSStuart Maybee
559b9bfdccdSStuart Maybee napics_found = 0;
560b9bfdccdSStuart Maybee
561b9bfdccdSStuart Maybee /*
562b9bfdccdSStuart Maybee * Search the MADT for ioapics
563b9bfdccdSStuart Maybee */
564b9bfdccdSStuart Maybee ap = (ACPI_SUBTABLE_HEADER *) (acpi_mapic_dtp + 1);
565b9bfdccdSStuart Maybee madt_size = acpi_mapic_dtp->Header.Length;
566b9bfdccdSStuart Maybee madt_seen = sizeof (*acpi_mapic_dtp);
567b9bfdccdSStuart Maybee
568b9bfdccdSStuart Maybee while (madt_seen < madt_size) {
569b9bfdccdSStuart Maybee
570b9bfdccdSStuart Maybee switch (ap->Type) {
571b9bfdccdSStuart Maybee case ACPI_MADT_TYPE_IO_APIC:
572b9bfdccdSStuart Maybee mia = (ACPI_MADT_IO_APIC *) ap;
573b9bfdccdSStuart Maybee if (napics_found < MAX_IO_APIC) {
574b9bfdccdSStuart Maybee ioapic_paddr[napics_found++] =
575b9bfdccdSStuart Maybee (ACPI_PHYSICAL_ADDRESS)
576b9bfdccdSStuart Maybee (mia->Address & PAGEMASK);
577b9bfdccdSStuart Maybee }
578b9bfdccdSStuart Maybee break;
579b9bfdccdSStuart Maybee
580b9bfdccdSStuart Maybee default:
581b9bfdccdSStuart Maybee break;
582b9bfdccdSStuart Maybee }
583b9bfdccdSStuart Maybee
584b9bfdccdSStuart Maybee /* advance to next entry */
585b9bfdccdSStuart Maybee madt_seen += ap->Length;
586b9bfdccdSStuart Maybee ap = (ACPI_SUBTABLE_HEADER *)(((char *)ap) + ap->Length);
587b9bfdccdSStuart Maybee }
588b9bfdccdSStuart Maybee if (dummy_ioapicadr == NULL)
589b9bfdccdSStuart Maybee dummy_ioapicadr = kmem_zalloc(PAGESIZE, KM_SLEEP);
590b9bfdccdSStuart Maybee }
591b9bfdccdSStuart Maybee
592b9bfdccdSStuart Maybee
593db2bae30SDana Myers void *
AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress,ACPI_SIZE Size)594db2bae30SDana Myers AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Size)
5957c478bd9Sstevel@tonic-gate {
596b9bfdccdSStuart Maybee int i;
597b9bfdccdSStuart Maybee
598b9bfdccdSStuart Maybee /*
599b9bfdccdSStuart Maybee * If the iopaic address table is populated, check if trying
600b9bfdccdSStuart Maybee * to access an ioapic. Instead, return a pointer to a dummy ioapic.
601b9bfdccdSStuart Maybee */
602b9bfdccdSStuart Maybee for (i = 0; i < napics_found; i++) {
603b9bfdccdSStuart Maybee if ((PhysicalAddress & PAGEMASK) == ioapic_paddr[i])
604b9bfdccdSStuart Maybee return (dummy_ioapicadr);
605b9bfdccdSStuart Maybee }
6067c478bd9Sstevel@tonic-gate /* FUTUREWORK: test PhysicalAddress for > 32 bits */
607db2bae30SDana Myers return (psm_map_new((paddr_t)PhysicalAddress,
608db2bae30SDana Myers (size_t)Size, PSM_PROT_WRITE | PSM_PROT_READ));
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate
6117c478bd9Sstevel@tonic-gate void
AcpiOsUnmapMemory(void * LogicalAddress,ACPI_SIZE Size)6127c478bd9Sstevel@tonic-gate AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Size)
6137c478bd9Sstevel@tonic-gate {
614b9bfdccdSStuart Maybee /*
615b9bfdccdSStuart Maybee * Check if trying to unmap dummy ioapic address.
616b9bfdccdSStuart Maybee */
617b9bfdccdSStuart Maybee if (LogicalAddress == dummy_ioapicadr)
618b9bfdccdSStuart Maybee return;
6197c478bd9Sstevel@tonic-gate
6207c478bd9Sstevel@tonic-gate psm_unmap((caddr_t)LogicalAddress, (size_t)Size);
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate
6237c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6247c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsGetPhysicalAddress(void * LogicalAddress,ACPI_PHYSICAL_ADDRESS * PhysicalAddress)6257c478bd9Sstevel@tonic-gate AcpiOsGetPhysicalAddress(void *LogicalAddress,
6261738dd6eSToomas Soome ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
6277c478bd9Sstevel@tonic-gate {
6287c478bd9Sstevel@tonic-gate
6297c478bd9Sstevel@tonic-gate /* UNIMPLEMENTED: not invoked by ACPI CA code */
63003335b7aSmyers return (AE_NOT_IMPLEMENTED);
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate
6337c478bd9Sstevel@tonic-gate
6347c478bd9Sstevel@tonic-gate ACPI_OSD_HANDLER acpi_isr;
6357c478bd9Sstevel@tonic-gate void *acpi_isr_context;
6367c478bd9Sstevel@tonic-gate
6377c478bd9Sstevel@tonic-gate uint_t
acpi_wrapper_isr(char * arg,char * arg1 __unused)63816d00323SToomas Soome acpi_wrapper_isr(char *arg, char *arg1 __unused)
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(arg))
6417c478bd9Sstevel@tonic-gate
6427c478bd9Sstevel@tonic-gate int status;
6437c478bd9Sstevel@tonic-gate
6447c478bd9Sstevel@tonic-gate status = (*acpi_isr)(acpi_isr_context);
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate if (status == ACPI_INTERRUPT_HANDLED) {
6477c478bd9Sstevel@tonic-gate return (DDI_INTR_CLAIMED);
6487c478bd9Sstevel@tonic-gate } else {
6497c478bd9Sstevel@tonic-gate return (DDI_INTR_UNCLAIMED);
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate
653186507a7Smyers static int acpi_intr_hooked = 0;
6547c478bd9Sstevel@tonic-gate
6557c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsInstallInterruptHandler(UINT32 InterruptNumber,ACPI_OSD_HANDLER ServiceRoutine,void * Context)6567c478bd9Sstevel@tonic-gate AcpiOsInstallInterruptHandler(UINT32 InterruptNumber,
6571738dd6eSToomas Soome ACPI_OSD_HANDLER ServiceRoutine,
6581738dd6eSToomas Soome void *Context)
6597c478bd9Sstevel@tonic-gate {
66027f7c583Smyers _NOTE(ARGUNUSED(InterruptNumber))
66127f7c583Smyers
6627c478bd9Sstevel@tonic-gate int retval;
663450d6964Smyers int sci_vect;
664450d6964Smyers iflag_t sci_flags;
6657c478bd9Sstevel@tonic-gate
6667c478bd9Sstevel@tonic-gate acpi_isr = ServiceRoutine;
6677c478bd9Sstevel@tonic-gate acpi_isr_context = Context;
6687c478bd9Sstevel@tonic-gate
6697c478bd9Sstevel@tonic-gate /*
670450d6964Smyers * Get SCI (adjusted for PIC/APIC mode if necessary)
6717c478bd9Sstevel@tonic-gate */
672450d6964Smyers if (acpica_get_sci(&sci_vect, &sci_flags) != AE_OK) {
673450d6964Smyers return (AE_ERROR);
674450d6964Smyers }
675450d6964Smyers
676450d6964Smyers #ifdef DEBUG
677450d6964Smyers cmn_err(CE_NOTE, "!acpica: attaching SCI %d", sci_vect);
678450d6964Smyers #endif
679450d6964Smyers
68016d00323SToomas Soome retval = add_avintr(NULL, SCI_IPL, acpi_wrapper_isr,
6815dff2f34Smyers "ACPI SCI", sci_vect, NULL, NULL, NULL, NULL);
6827c478bd9Sstevel@tonic-gate if (retval) {
683186507a7Smyers acpi_intr_hooked = 1;
6847c478bd9Sstevel@tonic-gate return (AE_OK);
685186507a7Smyers } else
6867c478bd9Sstevel@tonic-gate return (AE_BAD_PARAMETER);
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate
6897c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsRemoveInterruptHandler(UINT32 InterruptNumber,ACPI_OSD_HANDLER ServiceRoutine)6907c478bd9Sstevel@tonic-gate AcpiOsRemoveInterruptHandler(UINT32 InterruptNumber,
6911738dd6eSToomas Soome ACPI_OSD_HANDLER ServiceRoutine)
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(ServiceRoutine))
6947c478bd9Sstevel@tonic-gate
6957c478bd9Sstevel@tonic-gate #ifdef DEBUG
6967c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "!acpica: detaching SCI %d", InterruptNumber);
6977c478bd9Sstevel@tonic-gate #endif
698186507a7Smyers if (acpi_intr_hooked) {
69916d00323SToomas Soome rem_avintr(NULL, LOCK_LEVEL - 1, acpi_wrapper_isr,
7005dff2f34Smyers InterruptNumber);
701186507a7Smyers acpi_intr_hooked = 0;
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate return (AE_OK);
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate
7067c478bd9Sstevel@tonic-gate
70727f7c583Smyers ACPI_THREAD_ID
AcpiOsGetThreadId(void)7087c478bd9Sstevel@tonic-gate AcpiOsGetThreadId(void)
7097c478bd9Sstevel@tonic-gate {
71027f7c583Smyers /*
711aa2aa9a6SDana Myers * ACPI CA doesn't care what actual value is returned as long
712aa2aa9a6SDana Myers * as it is non-zero and unique to each existing thread.
713aa2aa9a6SDana Myers * ACPI CA assumes that thread ID is castable to a pointer,
714aa2aa9a6SDana Myers * so we use the current thread pointer.
71527f7c583Smyers */
71626f3cdf0SGordon Ross return (ACPI_CAST_PTHREAD_T((uintptr_t)curthread));
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate
7197c478bd9Sstevel@tonic-gate /*
7207c478bd9Sstevel@tonic-gate *
7217c478bd9Sstevel@tonic-gate */
7227c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsExecute(ACPI_EXECUTE_TYPE Type,ACPI_OSD_EXEC_CALLBACK Function,void * Context)72327f7c583Smyers AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function,
72427f7c583Smyers void *Context)
7257c478bd9Sstevel@tonic-gate {
7267c478bd9Sstevel@tonic-gate
7277c478bd9Sstevel@tonic-gate if (!acpica_eventq_init) {
7287c478bd9Sstevel@tonic-gate /*
7297c478bd9Sstevel@tonic-gate * Create taskqs for event handling
7307c478bd9Sstevel@tonic-gate */
7317c478bd9Sstevel@tonic-gate if (init_event_queues() != AE_OK)
7325dff2f34Smyers return (AE_ERROR);
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate
73527f7c583Smyers if (ddi_taskq_dispatch(osl_eventq[Type], Function, Context,
73627f7c583Smyers DDI_NOSLEEP) == DDI_FAILURE) {
7377c478bd9Sstevel@tonic-gate #ifdef DEBUG
7387c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "!acpica: unable to dispatch event");
7397c478bd9Sstevel@tonic-gate #endif
7407c478bd9Sstevel@tonic-gate return (AE_ERROR);
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate return (AE_OK);
7437c478bd9Sstevel@tonic-gate
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate
7467b1019a6SJerry Jelinek
7477b1019a6SJerry Jelinek void
AcpiOsWaitEventsComplete(void)7487b1019a6SJerry Jelinek AcpiOsWaitEventsComplete(void)
7497b1019a6SJerry Jelinek {
7507b1019a6SJerry Jelinek int i;
7517b1019a6SJerry Jelinek
7527b1019a6SJerry Jelinek /*
7537b1019a6SJerry Jelinek * Wait for event queues to be empty.
7547b1019a6SJerry Jelinek */
7557b1019a6SJerry Jelinek for (i = OSL_GLOBAL_LOCK_HANDLER; i <= OSL_EC_BURST_HANDLER; i++) {
7567b1019a6SJerry Jelinek if (osl_eventq[i] != NULL) {
7577b1019a6SJerry Jelinek ddi_taskq_wait(osl_eventq[i]);
7587b1019a6SJerry Jelinek }
7597b1019a6SJerry Jelinek }
7607b1019a6SJerry Jelinek }
7617b1019a6SJerry Jelinek
7627c478bd9Sstevel@tonic-gate void
AcpiOsSleep(ACPI_INTEGER Milliseconds)7637c478bd9Sstevel@tonic-gate AcpiOsSleep(ACPI_INTEGER Milliseconds)
7647c478bd9Sstevel@tonic-gate {
7657c478bd9Sstevel@tonic-gate /*
766137e2f25SGuoli Shu * During kernel startup, before the first tick interrupt
767137e2f25SGuoli Shu * has taken place, we can't call delay; very late in
768137e2f25SGuoli Shu * kernel shutdown or suspend/resume, clock interrupts
769859dcd3dSmyers * are blocked, so delay doesn't work then either.
770859dcd3dSmyers * So we busy wait if lbolt == 0 (kernel startup)
771137e2f25SGuoli Shu * or if acpica_use_safe_delay has been set to a
772137e2f25SGuoli Shu * non-zero value.
7737c478bd9Sstevel@tonic-gate */
774137e2f25SGuoli Shu if ((ddi_get_lbolt() == 0) || acpica_use_safe_delay)
7757c478bd9Sstevel@tonic-gate drv_usecwait(Milliseconds * 1000);
7767c478bd9Sstevel@tonic-gate else
7777c478bd9Sstevel@tonic-gate delay(drv_usectohz(Milliseconds * 1000));
7787c478bd9Sstevel@tonic-gate }
7797c478bd9Sstevel@tonic-gate
7807c478bd9Sstevel@tonic-gate void
AcpiOsStall(UINT32 Microseconds)7817c478bd9Sstevel@tonic-gate AcpiOsStall(UINT32 Microseconds)
7827c478bd9Sstevel@tonic-gate {
7837c478bd9Sstevel@tonic-gate drv_usecwait(Microseconds);
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate
7867c478bd9Sstevel@tonic-gate
7877c478bd9Sstevel@tonic-gate /*
7887c478bd9Sstevel@tonic-gate * Implementation of "Windows 2001" compatible I/O permission map
7897c478bd9Sstevel@tonic-gate *
7907c478bd9Sstevel@tonic-gate */
7917c478bd9Sstevel@tonic-gate #define OSL_IO_NONE (0)
7927c478bd9Sstevel@tonic-gate #define OSL_IO_READ (1<<0)
7937c478bd9Sstevel@tonic-gate #define OSL_IO_WRITE (1<<1)
7947c478bd9Sstevel@tonic-gate #define OSL_IO_RW (OSL_IO_READ | OSL_IO_WRITE)
7957c478bd9Sstevel@tonic-gate #define OSL_IO_TERM (1<<2)
7967c478bd9Sstevel@tonic-gate #define OSL_IO_DEFAULT OSL_IO_RW
7977c478bd9Sstevel@tonic-gate
7987c478bd9Sstevel@tonic-gate static struct io_perm {
7997c478bd9Sstevel@tonic-gate ACPI_IO_ADDRESS low;
8007c478bd9Sstevel@tonic-gate ACPI_IO_ADDRESS high;
8017c478bd9Sstevel@tonic-gate uint8_t perm;
8027c478bd9Sstevel@tonic-gate } osl_io_perm[] = {
8034cf02d40SSaurabh Misra { 0xcf8, 0xd00, OSL_IO_TERM | OSL_IO_RW}
8047c478bd9Sstevel@tonic-gate };
8057c478bd9Sstevel@tonic-gate
8067c478bd9Sstevel@tonic-gate
8077c478bd9Sstevel@tonic-gate /*
8087c478bd9Sstevel@tonic-gate *
8097c478bd9Sstevel@tonic-gate */
8107c478bd9Sstevel@tonic-gate static struct io_perm *
osl_io_find_perm(ACPI_IO_ADDRESS addr)8117c478bd9Sstevel@tonic-gate osl_io_find_perm(ACPI_IO_ADDRESS addr)
8127c478bd9Sstevel@tonic-gate {
8137c478bd9Sstevel@tonic-gate struct io_perm *p;
8147c478bd9Sstevel@tonic-gate
8157c478bd9Sstevel@tonic-gate p = osl_io_perm;
8167c478bd9Sstevel@tonic-gate while (p != NULL) {
8177c478bd9Sstevel@tonic-gate if ((p->low <= addr) && (addr <= p->high))
8187c478bd9Sstevel@tonic-gate break;
8197c478bd9Sstevel@tonic-gate p = (p->perm & OSL_IO_TERM) ? NULL : p+1;
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate
8227c478bd9Sstevel@tonic-gate return (p);
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate
8257c478bd9Sstevel@tonic-gate /*
8267c478bd9Sstevel@tonic-gate *
8277c478bd9Sstevel@tonic-gate */
8287c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsReadPort(ACPI_IO_ADDRESS Address,UINT32 * Value,UINT32 Width)8297c478bd9Sstevel@tonic-gate AcpiOsReadPort(ACPI_IO_ADDRESS Address, UINT32 *Value, UINT32 Width)
8307c478bd9Sstevel@tonic-gate {
8317c478bd9Sstevel@tonic-gate struct io_perm *p;
8327c478bd9Sstevel@tonic-gate
8337c478bd9Sstevel@tonic-gate /* verify permission */
8347c478bd9Sstevel@tonic-gate p = osl_io_find_perm(Address);
8357c478bd9Sstevel@tonic-gate if (p && (p->perm & OSL_IO_READ) == 0) {
8367c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "!AcpiOsReadPort: %lx %u not permitted",
8375dff2f34Smyers (long)Address, Width);
8387c478bd9Sstevel@tonic-gate *Value = 0xffffffff;
8397c478bd9Sstevel@tonic-gate return (AE_ERROR);
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate
8427c478bd9Sstevel@tonic-gate switch (Width) {
8437c478bd9Sstevel@tonic-gate case 8:
8447c478bd9Sstevel@tonic-gate *Value = inb(Address);
8457c478bd9Sstevel@tonic-gate break;
8467c478bd9Sstevel@tonic-gate case 16:
8477c478bd9Sstevel@tonic-gate *Value = inw(Address);
8487c478bd9Sstevel@tonic-gate break;
8497c478bd9Sstevel@tonic-gate case 32:
8507c478bd9Sstevel@tonic-gate *Value = inl(Address);
8517c478bd9Sstevel@tonic-gate break;
8527c478bd9Sstevel@tonic-gate default:
8537c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "!AcpiOsReadPort: %lx %u failed",
8545dff2f34Smyers (long)Address, Width);
8557c478bd9Sstevel@tonic-gate return (AE_BAD_PARAMETER);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate return (AE_OK);
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate
8607c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsWritePort(ACPI_IO_ADDRESS Address,UINT32 Value,UINT32 Width)8617c478bd9Sstevel@tonic-gate AcpiOsWritePort(ACPI_IO_ADDRESS Address, UINT32 Value, UINT32 Width)
8627c478bd9Sstevel@tonic-gate {
8637c478bd9Sstevel@tonic-gate struct io_perm *p;
8647c478bd9Sstevel@tonic-gate
8657c478bd9Sstevel@tonic-gate /* verify permission */
8667c478bd9Sstevel@tonic-gate p = osl_io_find_perm(Address);
8677c478bd9Sstevel@tonic-gate if (p && (p->perm & OSL_IO_WRITE) == 0) {
8687c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "!AcpiOsWritePort: %lx %u not permitted",
8697c478bd9Sstevel@tonic-gate (long)Address, Width);
8707c478bd9Sstevel@tonic-gate return (AE_ERROR);
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate
8737c478bd9Sstevel@tonic-gate switch (Width) {
8747c478bd9Sstevel@tonic-gate case 8:
8757c478bd9Sstevel@tonic-gate outb(Address, Value);
8767c478bd9Sstevel@tonic-gate break;
8777c478bd9Sstevel@tonic-gate case 16:
8787c478bd9Sstevel@tonic-gate outw(Address, Value);
8797c478bd9Sstevel@tonic-gate break;
8807c478bd9Sstevel@tonic-gate case 32:
8817c478bd9Sstevel@tonic-gate outl(Address, Value);
8827c478bd9Sstevel@tonic-gate break;
8837c478bd9Sstevel@tonic-gate default:
8847c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "!AcpiOsWritePort: %lx %u failed",
8857c478bd9Sstevel@tonic-gate (long)Address, Width);
8867c478bd9Sstevel@tonic-gate return (AE_BAD_PARAMETER);
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate return (AE_OK);
8897c478bd9Sstevel@tonic-gate }
8907c478bd9Sstevel@tonic-gate
8917c478bd9Sstevel@tonic-gate
8927c478bd9Sstevel@tonic-gate /*
8937c478bd9Sstevel@tonic-gate *
8947c478bd9Sstevel@tonic-gate */
8957c478bd9Sstevel@tonic-gate
8967c478bd9Sstevel@tonic-gate #define OSL_RW(ptr, val, type, rw) \
8977c478bd9Sstevel@tonic-gate { if (rw) *((type *)(ptr)) = *((type *) val); \
8987c478bd9Sstevel@tonic-gate else *((type *) val) = *((type *)(ptr)); }
8997c478bd9Sstevel@tonic-gate
9007c478bd9Sstevel@tonic-gate
9017c478bd9Sstevel@tonic-gate static void
osl_rw_memory(ACPI_PHYSICAL_ADDRESS Address,UINT64 * Value,UINT32 Width,int write)9027b1019a6SJerry Jelinek osl_rw_memory(ACPI_PHYSICAL_ADDRESS Address, UINT64 *Value,
9037c478bd9Sstevel@tonic-gate UINT32 Width, int write)
9047c478bd9Sstevel@tonic-gate {
9057c478bd9Sstevel@tonic-gate size_t maplen = Width / 8;
9067c478bd9Sstevel@tonic-gate caddr_t ptr;
9077c478bd9Sstevel@tonic-gate
9087c478bd9Sstevel@tonic-gate ptr = psm_map_new((paddr_t)Address, maplen,
9097c478bd9Sstevel@tonic-gate PSM_PROT_WRITE | PSM_PROT_READ);
9107c478bd9Sstevel@tonic-gate
9117c478bd9Sstevel@tonic-gate switch (maplen) {
9127c478bd9Sstevel@tonic-gate case 1:
9137c478bd9Sstevel@tonic-gate OSL_RW(ptr, Value, uint8_t, write);
9147c478bd9Sstevel@tonic-gate break;
9157c478bd9Sstevel@tonic-gate case 2:
9167c478bd9Sstevel@tonic-gate OSL_RW(ptr, Value, uint16_t, write);
9177c478bd9Sstevel@tonic-gate break;
9187c478bd9Sstevel@tonic-gate case 4:
9197c478bd9Sstevel@tonic-gate OSL_RW(ptr, Value, uint32_t, write);
9207c478bd9Sstevel@tonic-gate break;
9217b1019a6SJerry Jelinek case 8:
9227b1019a6SJerry Jelinek OSL_RW(ptr, Value, uint64_t, write);
9237b1019a6SJerry Jelinek break;
9247c478bd9Sstevel@tonic-gate default:
9257c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "!osl_rw_memory: invalid size %d",
9267c478bd9Sstevel@tonic-gate Width);
9277c478bd9Sstevel@tonic-gate break;
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate
9307c478bd9Sstevel@tonic-gate psm_unmap(ptr, maplen);
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate
9337c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address,UINT64 * Value,UINT32 Width)9347c478bd9Sstevel@tonic-gate AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address,
9351738dd6eSToomas Soome UINT64 *Value, UINT32 Width)
9367c478bd9Sstevel@tonic-gate {
9377c478bd9Sstevel@tonic-gate osl_rw_memory(Address, Value, Width, 0);
9387c478bd9Sstevel@tonic-gate return (AE_OK);
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate
9417c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address,UINT64 Value,UINT32 Width)9427c478bd9Sstevel@tonic-gate AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address,
9431738dd6eSToomas Soome UINT64 Value, UINT32 Width)
9447c478bd9Sstevel@tonic-gate {
9457c478bd9Sstevel@tonic-gate osl_rw_memory(Address, &Value, Width, 1);
9467c478bd9Sstevel@tonic-gate return (AE_OK);
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate
9497c478bd9Sstevel@tonic-gate
9507c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsReadPciConfiguration(ACPI_PCI_ID * PciId,UINT32 Reg,UINT64 * Value,UINT32 Width)95126f3cdf0SGordon Ross AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Reg,
9521738dd6eSToomas Soome UINT64 *Value, UINT32 Width)
9537c478bd9Sstevel@tonic-gate {
9547c478bd9Sstevel@tonic-gate
9557c478bd9Sstevel@tonic-gate switch (Width) {
9567c478bd9Sstevel@tonic-gate case 8:
95726f3cdf0SGordon Ross *Value = (UINT64)(*pci_getb_func)
95826f3cdf0SGordon Ross (PciId->Bus, PciId->Device, PciId->Function, Reg);
9597c478bd9Sstevel@tonic-gate break;
9607c478bd9Sstevel@tonic-gate case 16:
96126f3cdf0SGordon Ross *Value = (UINT64)(*pci_getw_func)
96226f3cdf0SGordon Ross (PciId->Bus, PciId->Device, PciId->Function, Reg);
9637c478bd9Sstevel@tonic-gate break;
9647c478bd9Sstevel@tonic-gate case 32:
96526f3cdf0SGordon Ross *Value = (UINT64)(*pci_getl_func)
96626f3cdf0SGordon Ross (PciId->Bus, PciId->Device, PciId->Function, Reg);
9677c478bd9Sstevel@tonic-gate break;
9687c478bd9Sstevel@tonic-gate case 64:
9697c478bd9Sstevel@tonic-gate default:
9707c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "!AcpiOsReadPciConfiguration: %x %u failed",
97126f3cdf0SGordon Ross Reg, Width);
9727c478bd9Sstevel@tonic-gate return (AE_BAD_PARAMETER);
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate return (AE_OK);
9757c478bd9Sstevel@tonic-gate }
9767c478bd9Sstevel@tonic-gate
9777c478bd9Sstevel@tonic-gate /*
9787c478bd9Sstevel@tonic-gate *
9797c478bd9Sstevel@tonic-gate */
9807c478bd9Sstevel@tonic-gate int acpica_write_pci_config_ok = 1;
9817c478bd9Sstevel@tonic-gate
9827c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsWritePciConfiguration(ACPI_PCI_ID * PciId,UINT32 Reg,UINT64 Value,UINT32 Width)98326f3cdf0SGordon Ross AcpiOsWritePciConfiguration(ACPI_PCI_ID *PciId, UINT32 Reg,
9841738dd6eSToomas Soome UINT64 Value, UINT32 Width)
9857c478bd9Sstevel@tonic-gate {
9867c478bd9Sstevel@tonic-gate
9877c478bd9Sstevel@tonic-gate if (!acpica_write_pci_config_ok) {
9887c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "!write to PCI cfg %x/%x/%x %x"
9897c478bd9Sstevel@tonic-gate " %lx %d not permitted", PciId->Bus, PciId->Device,
99026f3cdf0SGordon Ross PciId->Function, Reg, (long)Value, Width);
9917c478bd9Sstevel@tonic-gate return (AE_OK);
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate
9947c478bd9Sstevel@tonic-gate switch (Width) {
9957c478bd9Sstevel@tonic-gate case 8:
9967c478bd9Sstevel@tonic-gate (*pci_putb_func)(PciId->Bus, PciId->Device, PciId->Function,
99726f3cdf0SGordon Ross Reg, (uint8_t)Value);
9987c478bd9Sstevel@tonic-gate break;
9997c478bd9Sstevel@tonic-gate case 16:
10007c478bd9Sstevel@tonic-gate (*pci_putw_func)(PciId->Bus, PciId->Device, PciId->Function,
100126f3cdf0SGordon Ross Reg, (uint16_t)Value);
10027c478bd9Sstevel@tonic-gate break;
10037c478bd9Sstevel@tonic-gate case 32:
10047c478bd9Sstevel@tonic-gate (*pci_putl_func)(PciId->Bus, PciId->Device, PciId->Function,
100526f3cdf0SGordon Ross Reg, (uint32_t)Value);
10067c478bd9Sstevel@tonic-gate break;
10077c478bd9Sstevel@tonic-gate case 64:
10087c478bd9Sstevel@tonic-gate default:
10097c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "!AcpiOsWritePciConfiguration: %x %u failed",
101026f3cdf0SGordon Ross Reg, Width);
10117c478bd9Sstevel@tonic-gate return (AE_BAD_PARAMETER);
10127c478bd9Sstevel@tonic-gate }
10137c478bd9Sstevel@tonic-gate return (AE_OK);
10147c478bd9Sstevel@tonic-gate }
10157c478bd9Sstevel@tonic-gate
10167c478bd9Sstevel@tonic-gate /*
10177c478bd9Sstevel@tonic-gate * Called with ACPI_HANDLEs for both a PCI Config Space
10187c478bd9Sstevel@tonic-gate * OpRegion and (what ACPI CA thinks is) the PCI device
10195f68fde5SDana Myers * to which this ConfigSpace OpRegion belongs.
10207c478bd9Sstevel@tonic-gate *
10215f68fde5SDana Myers * ACPI CA uses _BBN and _ADR objects to determine the default
10225f68fde5SDana Myers * values for bus, segment, device and function; anything ACPI CA
10235f68fde5SDana Myers * can't figure out from the ACPI tables will be 0. One very
10245f68fde5SDana Myers * old 32-bit x86 system is known to have broken _BBN; this is
10255f68fde5SDana Myers * not addressed here.
10267c478bd9Sstevel@tonic-gate *
102727f7c583Smyers * Some BIOSes implement _BBN() by reading PCI config space
10285dff2f34Smyers * on bus #0 - which means that we'll recurse when we attempt
102927f7c583Smyers * to create the devinfo-to-ACPI map. If Derive is called during
10305cff7825Smh * scan_d2a_map, we don't translate the bus # and return.
10315cff7825Smh *
10325cff7825Smh * We get the parent of the OpRegion, which must be a PCI
10335cff7825Smh * node, fetch the associated devinfo node and snag the
10345cff7825Smh * b/d/f from it.
10357c478bd9Sstevel@tonic-gate */
10367c478bd9Sstevel@tonic-gate void
AcpiOsDerivePciId(ACPI_HANDLE rhandle,ACPI_HANDLE chandle,ACPI_PCI_ID ** PciId)10377c478bd9Sstevel@tonic-gate AcpiOsDerivePciId(ACPI_HANDLE rhandle, ACPI_HANDLE chandle,
10381738dd6eSToomas Soome ACPI_PCI_ID **PciId)
10397c478bd9Sstevel@tonic-gate {
10407c478bd9Sstevel@tonic-gate ACPI_HANDLE handle;
10415cff7825Smh dev_info_t *dip;
10425cff7825Smh int bus, device, func, devfn;
10437c478bd9Sstevel@tonic-gate
104427f7c583Smyers /*
10455cff7825Smh * See above - avoid recursing during scanning_d2a_map.
104627f7c583Smyers */
10475cff7825Smh if (scanning_d2a_map)
10487c478bd9Sstevel@tonic-gate return;
10497c478bd9Sstevel@tonic-gate
10507c478bd9Sstevel@tonic-gate /*
10515cff7825Smh * Get the OpRegion's parent
10527c478bd9Sstevel@tonic-gate */
10537c478bd9Sstevel@tonic-gate if (AcpiGetParent(chandle, &handle) != AE_OK)
10547c478bd9Sstevel@tonic-gate return;
10557c478bd9Sstevel@tonic-gate
10565cff7825Smh /*
10575cff7825Smh * If we've mapped the ACPI node to the devinfo
10585cff7825Smh * tree, use the devinfo reg property
10595cff7825Smh */
10605f68fde5SDana Myers if (ACPI_SUCCESS(acpica_get_devinfo(handle, &dip)) &&
10615f68fde5SDana Myers (acpica_get_bdf(dip, &bus, &device, &func) >= 0)) {
10625cff7825Smh (*PciId)->Bus = bus;
10635cff7825Smh (*PciId)->Device = device;
10645cff7825Smh (*PciId)->Function = func;
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate }
10677c478bd9Sstevel@tonic-gate
10687c478bd9Sstevel@tonic-gate
106927f7c583Smyers /*ARGSUSED*/
10707c478bd9Sstevel@tonic-gate BOOLEAN
AcpiOsReadable(void * Pointer,ACPI_SIZE Length)10717c478bd9Sstevel@tonic-gate AcpiOsReadable(void *Pointer, ACPI_SIZE Length)
10727c478bd9Sstevel@tonic-gate {
10737c478bd9Sstevel@tonic-gate
10747c478bd9Sstevel@tonic-gate /* Always says yes; all mapped memory assumed readable */
10757c478bd9Sstevel@tonic-gate return (1);
10767c478bd9Sstevel@tonic-gate }
10777c478bd9Sstevel@tonic-gate
107827f7c583Smyers /*ARGSUSED*/
10797c478bd9Sstevel@tonic-gate BOOLEAN
AcpiOsWritable(void * Pointer,ACPI_SIZE Length)10807c478bd9Sstevel@tonic-gate AcpiOsWritable(void *Pointer, ACPI_SIZE Length)
10817c478bd9Sstevel@tonic-gate {
10827c478bd9Sstevel@tonic-gate
10837c478bd9Sstevel@tonic-gate /* Always says yes; all mapped memory assumed writable */
10847c478bd9Sstevel@tonic-gate return (1);
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate
10877c478bd9Sstevel@tonic-gate UINT64
AcpiOsGetTimer(void)10887c478bd9Sstevel@tonic-gate AcpiOsGetTimer(void)
10897c478bd9Sstevel@tonic-gate {
109027f7c583Smyers /* gethrtime() returns 1nS resolution; convert to 100nS granules */
109127f7c583Smyers return ((gethrtime() + 50) / 100);
109227f7c583Smyers }
10937c478bd9Sstevel@tonic-gate
1094fa96bd91SMichael Corcoran static struct AcpiOSIFeature_s {
1095fa96bd91SMichael Corcoran uint64_t control_flag;
1096fa96bd91SMichael Corcoran const char *feature_name;
1097fa96bd91SMichael Corcoran } AcpiOSIFeatures[] = {
1098fa96bd91SMichael Corcoran { ACPI_FEATURE_OSI_MODULE, "Module Device" },
1099fa96bd91SMichael Corcoran { 0, "Processor Device" }
1100fa96bd91SMichael Corcoran };
1101fa96bd91SMichael Corcoran
110227f7c583Smyers /*ARGSUSED*/
110327f7c583Smyers ACPI_STATUS
AcpiOsValidateInterface(char * feature)1104fa96bd91SMichael Corcoran AcpiOsValidateInterface(char *feature)
110527f7c583Smyers {
1106fa96bd91SMichael Corcoran int i;
1107fa96bd91SMichael Corcoran
1108fa96bd91SMichael Corcoran ASSERT(feature != NULL);
1109fa96bd91SMichael Corcoran for (i = 0; i < sizeof (AcpiOSIFeatures) / sizeof (AcpiOSIFeatures[0]);
1110fa96bd91SMichael Corcoran i++) {
1111fa96bd91SMichael Corcoran if (strcmp(feature, AcpiOSIFeatures[i].feature_name) != 0) {
1112fa96bd91SMichael Corcoran continue;
1113fa96bd91SMichael Corcoran }
1114fa96bd91SMichael Corcoran /* Check whether required core features are available. */
1115fa96bd91SMichael Corcoran if (AcpiOSIFeatures[i].control_flag != 0 &&
1116fa96bd91SMichael Corcoran acpica_get_core_feature(AcpiOSIFeatures[i].control_flag) !=
1117fa96bd91SMichael Corcoran AcpiOSIFeatures[i].control_flag) {
1118fa96bd91SMichael Corcoran break;
1119fa96bd91SMichael Corcoran }
1120fa96bd91SMichael Corcoran /* Feature supported. */
1121fa96bd91SMichael Corcoran return (AE_OK);
1122fa96bd91SMichael Corcoran }
1123fa96bd91SMichael Corcoran
112427f7c583Smyers return (AE_SUPPORT);
112527f7c583Smyers }
11267c478bd9Sstevel@tonic-gate
112727f7c583Smyers /*ARGSUSED*/
112827f7c583Smyers ACPI_STATUS
AcpiOsValidateAddress(UINT8 spaceid,ACPI_PHYSICAL_ADDRESS addr,ACPI_SIZE length)112927f7c583Smyers AcpiOsValidateAddress(UINT8 spaceid, ACPI_PHYSICAL_ADDRESS addr,
113027f7c583Smyers ACPI_SIZE length)
113127f7c583Smyers {
113227f7c583Smyers return (AE_OK);
11337c478bd9Sstevel@tonic-gate }
11347c478bd9Sstevel@tonic-gate
11357c478bd9Sstevel@tonic-gate ACPI_STATUS
AcpiOsSignal(UINT32 Function,void * Info)11367c478bd9Sstevel@tonic-gate AcpiOsSignal(UINT32 Function, void *Info)
11377c478bd9Sstevel@tonic-gate {
11387c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(Function, Info))
11397c478bd9Sstevel@tonic-gate
11407c478bd9Sstevel@tonic-gate /* FUTUREWORK: debugger support */
11417c478bd9Sstevel@tonic-gate
11427c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "!OsSignal unimplemented");
11437c478bd9Sstevel@tonic-gate return (AE_OK);
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate
11467c478bd9Sstevel@tonic-gate void ACPI_INTERNAL_VAR_XFACE
AcpiOsPrintf(const char * Format,...)11477c478bd9Sstevel@tonic-gate AcpiOsPrintf(const char *Format, ...)
11487c478bd9Sstevel@tonic-gate {
11497c478bd9Sstevel@tonic-gate va_list ap;
1150186507a7Smyers
1151186507a7Smyers va_start(ap, Format);
1152186507a7Smyers AcpiOsVprintf(Format, ap);
1153186507a7Smyers va_end(ap);
1154186507a7Smyers }
1155186507a7Smyers
115635786f68SRobert Mustacchi /*ARGSUSED*/
115735786f68SRobert Mustacchi ACPI_STATUS
AcpiOsEnterSleep(UINT8 SleepState,UINT32 Rega,UINT32 Regb)115835786f68SRobert Mustacchi AcpiOsEnterSleep(UINT8 SleepState, UINT32 Rega, UINT32 Regb)
115935786f68SRobert Mustacchi {
116035786f68SRobert Mustacchi return (AE_OK);
116135786f68SRobert Mustacchi }
116235786f68SRobert Mustacchi
1163186507a7Smyers /*
1164186507a7Smyers * When != 0, sends output to console
1165186507a7Smyers * Patchable with kmdb or /etc/system.
1166186507a7Smyers */
1167186507a7Smyers int acpica_console_out = 0;
1168186507a7Smyers
1169186507a7Smyers #define ACPICA_OUTBUF_LEN 160
1170186507a7Smyers char acpica_outbuf[ACPICA_OUTBUF_LEN];
1171186507a7Smyers int acpica_outbuf_offset;
1172186507a7Smyers
1173186507a7Smyers /*
1174186507a7Smyers *
1175186507a7Smyers */
1176186507a7Smyers static void
acpica_pr_buf(char * buf)117787bb58d6Smyers acpica_pr_buf(char *buf)
1178186507a7Smyers {
1179186507a7Smyers char c, *bufp, *outp;
1180186507a7Smyers int out_remaining;
1181186507a7Smyers
1182186507a7Smyers /*
1183186507a7Smyers * copy the supplied buffer into the output buffer
1184186507a7Smyers * when we hit a '\n' or overflow the output buffer,
1185186507a7Smyers * output and reset the output buffer
1186186507a7Smyers */
1187186507a7Smyers bufp = buf;
1188186507a7Smyers outp = acpica_outbuf + acpica_outbuf_offset;
1189186507a7Smyers out_remaining = ACPICA_OUTBUF_LEN - acpica_outbuf_offset - 1;
1190186507a7Smyers while (c = *bufp++) {
1191186507a7Smyers *outp++ = c;
1192186507a7Smyers if (c == '\n' || --out_remaining == 0) {
1193186507a7Smyers *outp = '\0';
11949bb29364SDana Myers switch (acpica_console_out) {
11959bb29364SDana Myers case 1:
1196186507a7Smyers printf(acpica_outbuf);
11979bb29364SDana Myers break;
11989bb29364SDana Myers case 2:
11999bb29364SDana Myers prom_printf(acpica_outbuf);
12009bb29364SDana Myers break;
12019bb29364SDana Myers case 0:
12029bb29364SDana Myers default:
1203186507a7Smyers (void) strlog(0, 0, 0,
1204186507a7Smyers SL_CONSOLE | SL_NOTE | SL_LOGONLY,
1205186507a7Smyers acpica_outbuf);
12069bb29364SDana Myers break;
12079bb29364SDana Myers }
1208186507a7Smyers acpica_outbuf_offset = 0;
1209186507a7Smyers outp = acpica_outbuf;
1210186507a7Smyers out_remaining = ACPICA_OUTBUF_LEN - 1;
1211186507a7Smyers }
1212186507a7Smyers }
1213186507a7Smyers
1214186507a7Smyers acpica_outbuf_offset = outp - acpica_outbuf;
1215186507a7Smyers }
1216186507a7Smyers
1217186507a7Smyers void
AcpiOsVprintf(const char * Format,va_list Args)1218186507a7Smyers AcpiOsVprintf(const char *Format, va_list Args)
1219186507a7Smyers {
12207c478bd9Sstevel@tonic-gate
12217c478bd9Sstevel@tonic-gate /*
122287bb58d6Smyers * If AcpiOsInitialize() failed to allocate a string buffer,
122387bb58d6Smyers * resort to vprintf().
12247c478bd9Sstevel@tonic-gate */
122587bb58d6Smyers if (acpi_osl_pr_buffer == NULL) {
1226186507a7Smyers vprintf(Format, Args);
12277c478bd9Sstevel@tonic-gate return;
12287c478bd9Sstevel@tonic-gate }
122987bb58d6Smyers
123087bb58d6Smyers /*
123187bb58d6Smyers * It is possible that a very long debug output statement will
123287bb58d6Smyers * be truncated; this is silently ignored.
123387bb58d6Smyers */
123487bb58d6Smyers (void) vsnprintf(acpi_osl_pr_buffer, acpi_osl_pr_buflen, Format, Args);
123587bb58d6Smyers acpica_pr_buf(acpi_osl_pr_buffer);
12367c478bd9Sstevel@tonic-gate }
12377c478bd9Sstevel@tonic-gate
12387c478bd9Sstevel@tonic-gate void
AcpiOsRedirectOutput(void * Destination)12397c478bd9Sstevel@tonic-gate AcpiOsRedirectOutput(void *Destination)
12407c478bd9Sstevel@tonic-gate {
12417c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(Destination))
12427c478bd9Sstevel@tonic-gate
12437c478bd9Sstevel@tonic-gate /* FUTUREWORK: debugger support */
12447c478bd9Sstevel@tonic-gate
12457c478bd9Sstevel@tonic-gate #ifdef DEBUG
12467c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "!acpica: AcpiOsRedirectOutput called");
12477c478bd9Sstevel@tonic-gate #endif
12487c478bd9Sstevel@tonic-gate }
12497c478bd9Sstevel@tonic-gate
12507c478bd9Sstevel@tonic-gate
12517c478bd9Sstevel@tonic-gate UINT32
AcpiOsGetLine(char * Buffer,UINT32 len,UINT32 * BytesRead)125226f3cdf0SGordon Ross AcpiOsGetLine(char *Buffer, UINT32 len, UINT32 *BytesRead)
12537c478bd9Sstevel@tonic-gate {
12547c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(Buffer))
125526f3cdf0SGordon Ross _NOTE(ARGUNUSED(len))
125626f3cdf0SGordon Ross _NOTE(ARGUNUSED(BytesRead))
12577c478bd9Sstevel@tonic-gate
12587c478bd9Sstevel@tonic-gate /* FUTUREWORK: debugger support */
12597c478bd9Sstevel@tonic-gate
12607c478bd9Sstevel@tonic-gate return (0);
12617c478bd9Sstevel@tonic-gate }
12627c478bd9Sstevel@tonic-gate
1263*3349e36eSPaul Winder static ACPI_STATUS
acpica_crs_cb(ACPI_RESOURCE * rp,void * context)1264*3349e36eSPaul Winder acpica_crs_cb(ACPI_RESOURCE *rp, void *context)
1265*3349e36eSPaul Winder {
1266*3349e36eSPaul Winder int *busno = context;
1267*3349e36eSPaul Winder
1268*3349e36eSPaul Winder if (rp->Data.Address.ProducerConsumer == 1)
1269*3349e36eSPaul Winder return (AE_OK);
1270*3349e36eSPaul Winder
1271*3349e36eSPaul Winder switch (rp->Type) {
1272*3349e36eSPaul Winder case ACPI_RESOURCE_TYPE_ADDRESS16:
1273*3349e36eSPaul Winder if (rp->Data.Address16.Address.AddressLength == 0)
1274*3349e36eSPaul Winder break;
1275*3349e36eSPaul Winder if (rp->Data.Address16.ResourceType != ACPI_BUS_NUMBER_RANGE)
1276*3349e36eSPaul Winder break;
1277*3349e36eSPaul Winder
1278*3349e36eSPaul Winder *busno = rp->Data.Address16.Address.Minimum;
1279*3349e36eSPaul Winder break;
1280*3349e36eSPaul Winder
1281*3349e36eSPaul Winder case ACPI_RESOURCE_TYPE_ADDRESS32:
1282*3349e36eSPaul Winder if (rp->Data.Address32.Address.AddressLength == 0)
1283*3349e36eSPaul Winder break;
1284*3349e36eSPaul Winder if (rp->Data.Address32.ResourceType != ACPI_BUS_NUMBER_RANGE)
1285*3349e36eSPaul Winder break;
1286*3349e36eSPaul Winder
1287*3349e36eSPaul Winder *busno = rp->Data.Address32.Address.Minimum;
1288*3349e36eSPaul Winder break;
1289*3349e36eSPaul Winder
1290*3349e36eSPaul Winder case ACPI_RESOURCE_TYPE_ADDRESS64:
1291*3349e36eSPaul Winder if (rp->Data.Address64.Address.AddressLength == 0)
1292*3349e36eSPaul Winder break;
1293*3349e36eSPaul Winder if (rp->Data.Address64.ResourceType != ACPI_BUS_NUMBER_RANGE)
1294*3349e36eSPaul Winder break;
1295*3349e36eSPaul Winder
1296*3349e36eSPaul Winder *busno = (int)rp->Data.Address64.Address.Minimum;
1297*3349e36eSPaul Winder break;
1298*3349e36eSPaul Winder
1299*3349e36eSPaul Winder default:
1300*3349e36eSPaul Winder break;
1301*3349e36eSPaul Winder }
1302*3349e36eSPaul Winder
1303*3349e36eSPaul Winder return (AE_OK);
1304*3349e36eSPaul Winder }
1305*3349e36eSPaul Winder
1306*3349e36eSPaul Winder /*
1307*3349e36eSPaul Winder * Retrieve the bus number for a root bus.
1308*3349e36eSPaul Winder *
1309*3349e36eSPaul Winder * _CRS (Current Resource Setting) holds the bus number as set in
1310*3349e36eSPaul Winder * PCI configuration, this may differ from _BBN and is a more reliable
1311*3349e36eSPaul Winder * indicator of what the bus number is.
1312*3349e36eSPaul Winder */
1313*3349e36eSPaul Winder ACPI_STATUS
acpica_get_busno(ACPI_HANDLE hdl,int * busno)1314*3349e36eSPaul Winder acpica_get_busno(ACPI_HANDLE hdl, int *busno)
1315*3349e36eSPaul Winder {
1316*3349e36eSPaul Winder ACPI_STATUS rv;
1317*3349e36eSPaul Winder int bus = -1;
1318*3349e36eSPaul Winder int bbn;
1319*3349e36eSPaul Winder
1320*3349e36eSPaul Winder if (ACPI_FAILURE(rv = acpica_eval_int(hdl, "_BBN", &bbn)))
1321*3349e36eSPaul Winder return (rv);
1322*3349e36eSPaul Winder
1323*3349e36eSPaul Winder (void) AcpiWalkResources(hdl, "_CRS", acpica_crs_cb, &bus);
1324*3349e36eSPaul Winder
1325*3349e36eSPaul Winder *busno = bus == -1 ? bbn : bus;
1326*3349e36eSPaul Winder
1327*3349e36eSPaul Winder return (AE_OK);
1328*3349e36eSPaul Winder }
1329*3349e36eSPaul Winder
13307c478bd9Sstevel@tonic-gate /*
13317c478bd9Sstevel@tonic-gate * Device tree binding
13327c478bd9Sstevel@tonic-gate */
1333fa96bd91SMichael Corcoran static ACPI_STATUS
acpica_find_pcibus_walker(ACPI_HANDLE hdl,UINT32 lvl,void * ctxp,void ** rvpp)1334fa96bd91SMichael Corcoran acpica_find_pcibus_walker(ACPI_HANDLE hdl, UINT32 lvl, void *ctxp, void **rvpp)
1335fa96bd91SMichael Corcoran {
1336fa96bd91SMichael Corcoran _NOTE(ARGUNUSED(lvl));
1337fa96bd91SMichael Corcoran
1338fa96bd91SMichael Corcoran int sta, hid, bbn;
1339fa96bd91SMichael Corcoran int busno = (intptr_t)ctxp;
1340fa96bd91SMichael Corcoran ACPI_HANDLE *hdlp = (ACPI_HANDLE *)rvpp;
1341fa96bd91SMichael Corcoran
1342fa96bd91SMichael Corcoran /* Check whether device exists. */
1343fa96bd91SMichael Corcoran if (ACPI_SUCCESS(acpica_eval_int(hdl, "_STA", &sta)) &&
1344fa96bd91SMichael Corcoran !(sta & (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_FUNCTIONING))) {
1345fa96bd91SMichael Corcoran /*
1346fa96bd91SMichael Corcoran * Skip object if device doesn't exist.
1347fa96bd91SMichael Corcoran * According to ACPI Spec,
1348fa96bd91SMichael Corcoran * 1) setting either bit 0 or bit 3 means that device exists.
1349fa96bd91SMichael Corcoran * 2) Absence of _STA method means all status bits set.
1350fa96bd91SMichael Corcoran */
1351fa96bd91SMichael Corcoran return (AE_CTRL_DEPTH);
1352fa96bd91SMichael Corcoran }
1353fa96bd91SMichael Corcoran
1354fa96bd91SMichael Corcoran if (ACPI_FAILURE(acpica_eval_hid(hdl, "_HID", &hid)) ||
1355fa96bd91SMichael Corcoran (hid != HID_PCI_BUS && hid != HID_PCI_EXPRESS_BUS)) {
1356fa96bd91SMichael Corcoran /* Non PCI/PCIe host bridge. */
1357fa96bd91SMichael Corcoran return (AE_OK);
1358fa96bd91SMichael Corcoran }
1359fa96bd91SMichael Corcoran
1360fa96bd91SMichael Corcoran if (acpi_has_broken_bbn) {
1361fa96bd91SMichael Corcoran ACPI_BUFFER rb;
1362fa96bd91SMichael Corcoran rb.Pointer = NULL;
1363fa96bd91SMichael Corcoran rb.Length = ACPI_ALLOCATE_BUFFER;
1364fa96bd91SMichael Corcoran
1365fa96bd91SMichael Corcoran /* Decree _BBN == n from PCI<n> */
1366fa96bd91SMichael Corcoran if (AcpiGetName(hdl, ACPI_SINGLE_NAME, &rb) != AE_OK) {
1367fa96bd91SMichael Corcoran return (AE_CTRL_TERMINATE);
1368fa96bd91SMichael Corcoran }
1369fa96bd91SMichael Corcoran bbn = ((char *)rb.Pointer)[3] - '0';
1370fa96bd91SMichael Corcoran AcpiOsFree(rb.Pointer);
1371fa96bd91SMichael Corcoran if (bbn == busno || busno == 0) {
1372fa96bd91SMichael Corcoran *hdlp = hdl;
1373fa96bd91SMichael Corcoran return (AE_CTRL_TERMINATE);
1374fa96bd91SMichael Corcoran }
1375*3349e36eSPaul Winder } else if (ACPI_SUCCESS(acpica_get_busno(hdl, &bbn))) {
1376fa96bd91SMichael Corcoran if (bbn == busno) {
1377fa96bd91SMichael Corcoran *hdlp = hdl;
1378fa96bd91SMichael Corcoran return (AE_CTRL_TERMINATE);
1379fa96bd91SMichael Corcoran }
1380fa96bd91SMichael Corcoran } else if (busno == 0) {
1381fa96bd91SMichael Corcoran *hdlp = hdl;
1382fa96bd91SMichael Corcoran return (AE_CTRL_TERMINATE);
1383fa96bd91SMichael Corcoran }
1384fa96bd91SMichael Corcoran
1385fa96bd91SMichael Corcoran return (AE_CTRL_DEPTH);
1386fa96bd91SMichael Corcoran }
13877c478bd9Sstevel@tonic-gate
13887c478bd9Sstevel@tonic-gate static int
acpica_find_pcibus(int busno,ACPI_HANDLE * rh)13897c478bd9Sstevel@tonic-gate acpica_find_pcibus(int busno, ACPI_HANDLE *rh)
13907c478bd9Sstevel@tonic-gate {
13917c478bd9Sstevel@tonic-gate ACPI_HANDLE sbobj, busobj;
13927c478bd9Sstevel@tonic-gate
13937c478bd9Sstevel@tonic-gate /* initialize static flag by querying ACPI namespace for bug */
13947c478bd9Sstevel@tonic-gate if (acpi_has_broken_bbn == -1)
13957c478bd9Sstevel@tonic-gate acpi_has_broken_bbn = acpica_query_bbn_problem();
13967c478bd9Sstevel@tonic-gate
1397fa96bd91SMichael Corcoran if (ACPI_SUCCESS(AcpiGetHandle(NULL, "\\_SB", &sbobj))) {
1398fa96bd91SMichael Corcoran busobj = NULL;
1399fa96bd91SMichael Corcoran (void) AcpiWalkNamespace(ACPI_TYPE_DEVICE, sbobj, UINT32_MAX,
140057190917SDana Myers acpica_find_pcibus_walker, NULL, (void *)(intptr_t)busno,
1401fa96bd91SMichael Corcoran (void **)&busobj);
1402fa96bd91SMichael Corcoran if (busobj != NULL) {
1403fa96bd91SMichael Corcoran *rh = busobj;
1404fa96bd91SMichael Corcoran return (AE_OK);
14057c478bd9Sstevel@tonic-gate }
14067c478bd9Sstevel@tonic-gate }
1407fa96bd91SMichael Corcoran
14087c478bd9Sstevel@tonic-gate return (AE_ERROR);
14097c478bd9Sstevel@tonic-gate }
14107c478bd9Sstevel@tonic-gate
1411fa96bd91SMichael Corcoran static ACPI_STATUS
acpica_query_bbn_walker(ACPI_HANDLE hdl,UINT32 lvl,void * ctxp,void ** rvpp)1412fa96bd91SMichael Corcoran acpica_query_bbn_walker(ACPI_HANDLE hdl, UINT32 lvl, void *ctxp, void **rvpp)
1413fa96bd91SMichael Corcoran {
1414fa96bd91SMichael Corcoran _NOTE(ARGUNUSED(lvl));
1415fa96bd91SMichael Corcoran _NOTE(ARGUNUSED(rvpp));
1416fa96bd91SMichael Corcoran
1417fa96bd91SMichael Corcoran int sta, hid, bbn;
1418fa96bd91SMichael Corcoran int *cntp = (int *)ctxp;
1419fa96bd91SMichael Corcoran
1420fa96bd91SMichael Corcoran /* Check whether device exists. */
1421fa96bd91SMichael Corcoran if (ACPI_SUCCESS(acpica_eval_int(hdl, "_STA", &sta)) &&
1422fa96bd91SMichael Corcoran !(sta & (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_FUNCTIONING))) {
1423fa96bd91SMichael Corcoran /*
1424fa96bd91SMichael Corcoran * Skip object if device doesn't exist.
1425fa96bd91SMichael Corcoran * According to ACPI Spec,
1426fa96bd91SMichael Corcoran * 1) setting either bit 0 or bit 3 means that device exists.
1427fa96bd91SMichael Corcoran * 2) Absence of _STA method means all status bits set.
1428fa96bd91SMichael Corcoran */
1429fa96bd91SMichael Corcoran return (AE_CTRL_DEPTH);
1430fa96bd91SMichael Corcoran }
1431fa96bd91SMichael Corcoran
1432fa96bd91SMichael Corcoran if (ACPI_FAILURE(acpica_eval_hid(hdl, "_HID", &hid)) ||
1433fa96bd91SMichael Corcoran (hid != HID_PCI_BUS && hid != HID_PCI_EXPRESS_BUS)) {
1434fa96bd91SMichael Corcoran /* Non PCI/PCIe host bridge. */
1435fa96bd91SMichael Corcoran return (AE_OK);
1436fa96bd91SMichael Corcoran } else if (ACPI_SUCCESS(acpica_eval_int(hdl, "_BBN", &bbn)) &&
1437fa96bd91SMichael Corcoran bbn == 0 && ++(*cntp) > 1) {
1438fa96bd91SMichael Corcoran /*
1439fa96bd91SMichael Corcoran * If we find more than one bus with a 0 _BBN
1440fa96bd91SMichael Corcoran * we have the problem that BigBear's BIOS shows
1441fa96bd91SMichael Corcoran */
1442fa96bd91SMichael Corcoran return (AE_CTRL_TERMINATE);
1443fa96bd91SMichael Corcoran } else {
1444fa96bd91SMichael Corcoran /*
1445fa96bd91SMichael Corcoran * Skip children of PCI/PCIe host bridge.
1446fa96bd91SMichael Corcoran */
1447fa96bd91SMichael Corcoran return (AE_CTRL_DEPTH);
1448fa96bd91SMichael Corcoran }
1449fa96bd91SMichael Corcoran }
14507c478bd9Sstevel@tonic-gate
14517c478bd9Sstevel@tonic-gate /*
14527c478bd9Sstevel@tonic-gate * Look for ACPI problem where _BBN is zero for multiple PCI buses
14537c478bd9Sstevel@tonic-gate * This is a clear ACPI bug, but we have a workaround in acpica_find_pcibus()
14547c478bd9Sstevel@tonic-gate * below if it exists.
14557c478bd9Sstevel@tonic-gate */
14567c478bd9Sstevel@tonic-gate static int
acpica_query_bbn_problem(void)14577c478bd9Sstevel@tonic-gate acpica_query_bbn_problem(void)
14587c478bd9Sstevel@tonic-gate {
1459fa96bd91SMichael Corcoran ACPI_HANDLE sbobj;
14607c478bd9Sstevel@tonic-gate int zerobbncnt;
1461fa96bd91SMichael Corcoran void *rv;
14627c478bd9Sstevel@tonic-gate
14637c478bd9Sstevel@tonic-gate zerobbncnt = 0;
1464fa96bd91SMichael Corcoran if (ACPI_SUCCESS(AcpiGetHandle(NULL, "\\_SB", &sbobj))) {
1465fa96bd91SMichael Corcoran (void) AcpiWalkNamespace(ACPI_TYPE_DEVICE, sbobj, UINT32_MAX,
146657190917SDana Myers acpica_query_bbn_walker, NULL, &zerobbncnt, &rv);
14677c478bd9Sstevel@tonic-gate }
1468fa96bd91SMichael Corcoran
1469fa96bd91SMichael Corcoran return (zerobbncnt > 1 ? 1 : 0);
14707c478bd9Sstevel@tonic-gate }
14717c478bd9Sstevel@tonic-gate
14727c478bd9Sstevel@tonic-gate static const char hextab[] = "0123456789ABCDEF";
14737c478bd9Sstevel@tonic-gate
14747c478bd9Sstevel@tonic-gate static int
hexdig(int c)14757c478bd9Sstevel@tonic-gate hexdig(int c)
14767c478bd9Sstevel@tonic-gate {
14777c478bd9Sstevel@tonic-gate /*
14787c478bd9Sstevel@tonic-gate * Get hex digit:
14797c478bd9Sstevel@tonic-gate *
14807c478bd9Sstevel@tonic-gate * Returns the 4-bit hex digit named by the input character. Returns
14817c478bd9Sstevel@tonic-gate * zero if the input character is not valid hex!
14827c478bd9Sstevel@tonic-gate */
14837c478bd9Sstevel@tonic-gate
14847c478bd9Sstevel@tonic-gate int x = ((c < 'a') || (c > 'z')) ? c : (c - ' ');
14857c478bd9Sstevel@tonic-gate int j = sizeof (hextab);
14867c478bd9Sstevel@tonic-gate
14875dff2f34Smyers while (--j && (x != hextab[j])) {
14885dff2f34Smyers }
14897c478bd9Sstevel@tonic-gate return (j);
14907c478bd9Sstevel@tonic-gate }
14917c478bd9Sstevel@tonic-gate
14927c478bd9Sstevel@tonic-gate static int
CompressEisaID(char * np)14937c478bd9Sstevel@tonic-gate CompressEisaID(char *np)
14947c478bd9Sstevel@tonic-gate {
14957c478bd9Sstevel@tonic-gate /*
14967c478bd9Sstevel@tonic-gate * Compress an EISA device name:
14977c478bd9Sstevel@tonic-gate *
14987c478bd9Sstevel@tonic-gate * This routine converts a 7-byte ASCII device name into the 4-byte
14997c478bd9Sstevel@tonic-gate * compressed form used by EISA (50 bytes of ROM to save 1 byte of
15007c478bd9Sstevel@tonic-gate * NV-RAM!)
15017c478bd9Sstevel@tonic-gate */
15027c478bd9Sstevel@tonic-gate
15037c478bd9Sstevel@tonic-gate union { char octets[4]; int retval; } myu;
15047c478bd9Sstevel@tonic-gate
15057c478bd9Sstevel@tonic-gate myu.octets[0] = ((np[0] & 0x1F) << 2) + ((np[1] >> 3) & 0x03);
15067c478bd9Sstevel@tonic-gate myu.octets[1] = ((np[1] & 0x07) << 5) + (np[2] & 0x1F);
15077c478bd9Sstevel@tonic-gate myu.octets[2] = (hexdig(np[3]) << 4) + hexdig(np[4]);
15087c478bd9Sstevel@tonic-gate myu.octets[3] = (hexdig(np[5]) << 4) + hexdig(np[6]);
15097c478bd9Sstevel@tonic-gate
15107c478bd9Sstevel@tonic-gate return (myu.retval);
15117c478bd9Sstevel@tonic-gate }
15127c478bd9Sstevel@tonic-gate
15135cff7825Smh ACPI_STATUS
acpica_eval_int(ACPI_HANDLE dev,char * method,int * rint)15147c478bd9Sstevel@tonic-gate acpica_eval_int(ACPI_HANDLE dev, char *method, int *rint)
15157c478bd9Sstevel@tonic-gate {
15167c478bd9Sstevel@tonic-gate ACPI_STATUS status;
15177c478bd9Sstevel@tonic-gate ACPI_BUFFER rb;
15187c478bd9Sstevel@tonic-gate ACPI_OBJECT ro;
15197c478bd9Sstevel@tonic-gate
15207c478bd9Sstevel@tonic-gate rb.Pointer = &ro;
15217c478bd9Sstevel@tonic-gate rb.Length = sizeof (ro);
15227c478bd9Sstevel@tonic-gate if ((status = AcpiEvaluateObjectTyped(dev, method, NULL, &rb,
15235dff2f34Smyers ACPI_TYPE_INTEGER)) == AE_OK)
15247c478bd9Sstevel@tonic-gate *rint = ro.Integer.Value;
15257c478bd9Sstevel@tonic-gate
15267c478bd9Sstevel@tonic-gate return (status);
15277c478bd9Sstevel@tonic-gate }
15287c478bd9Sstevel@tonic-gate
15297c478bd9Sstevel@tonic-gate static int
acpica_eval_hid(ACPI_HANDLE dev,char * method,int * rint)15307c478bd9Sstevel@tonic-gate acpica_eval_hid(ACPI_HANDLE dev, char *method, int *rint)
15317c478bd9Sstevel@tonic-gate {
15327c478bd9Sstevel@tonic-gate ACPI_BUFFER rb;
15337c478bd9Sstevel@tonic-gate ACPI_OBJECT *rv;
15347c478bd9Sstevel@tonic-gate
15357c478bd9Sstevel@tonic-gate rb.Pointer = NULL;
15367c478bd9Sstevel@tonic-gate rb.Length = ACPI_ALLOCATE_BUFFER;
1537db2bae30SDana Myers if (AcpiEvaluateObject(dev, method, NULL, &rb) == AE_OK &&
1538db2bae30SDana Myers rb.Length != 0) {
15397c478bd9Sstevel@tonic-gate rv = rb.Pointer;
15407c478bd9Sstevel@tonic-gate if (rv->Type == ACPI_TYPE_INTEGER) {
15417c478bd9Sstevel@tonic-gate *rint = rv->Integer.Value;
15427c478bd9Sstevel@tonic-gate AcpiOsFree(rv);
15437c478bd9Sstevel@tonic-gate return (AE_OK);
15447c478bd9Sstevel@tonic-gate } else if (rv->Type == ACPI_TYPE_STRING) {
15457c478bd9Sstevel@tonic-gate char *stringData;
15467c478bd9Sstevel@tonic-gate
15477c478bd9Sstevel@tonic-gate /* Convert the string into an EISA ID */
15487c478bd9Sstevel@tonic-gate if (rv->String.Pointer == NULL) {
15497c478bd9Sstevel@tonic-gate AcpiOsFree(rv);
15507c478bd9Sstevel@tonic-gate return (AE_ERROR);
15517c478bd9Sstevel@tonic-gate }
15527c478bd9Sstevel@tonic-gate
15537c478bd9Sstevel@tonic-gate stringData = rv->String.Pointer;
15547c478bd9Sstevel@tonic-gate
15557c478bd9Sstevel@tonic-gate /*
15567c478bd9Sstevel@tonic-gate * If the string is an EisaID, it must be 7
15577c478bd9Sstevel@tonic-gate * characters; if it's an ACPI ID, it will be 8
15587c478bd9Sstevel@tonic-gate * (and we don't care about ACPI ids here).
15597c478bd9Sstevel@tonic-gate */
15607c478bd9Sstevel@tonic-gate if (strlen(stringData) != 7) {
15617c478bd9Sstevel@tonic-gate AcpiOsFree(rv);
15627c478bd9Sstevel@tonic-gate return (AE_ERROR);
15637c478bd9Sstevel@tonic-gate }
15647c478bd9Sstevel@tonic-gate
15657c478bd9Sstevel@tonic-gate *rint = CompressEisaID(stringData);
15667c478bd9Sstevel@tonic-gate AcpiOsFree(rv);
15677c478bd9Sstevel@tonic-gate return (AE_OK);
15687c478bd9Sstevel@tonic-gate } else
15697c478bd9Sstevel@tonic-gate AcpiOsFree(rv);
15707c478bd9Sstevel@tonic-gate }
15717c478bd9Sstevel@tonic-gate return (AE_ERROR);
15727c478bd9Sstevel@tonic-gate }
15737c478bd9Sstevel@tonic-gate
15747c478bd9Sstevel@tonic-gate /*
15755cff7825Smh * Create linkage between devinfo nodes and ACPI nodes
15767c478bd9Sstevel@tonic-gate */
1577fa96bd91SMichael Corcoran ACPI_STATUS
acpica_tag_devinfo(dev_info_t * dip,ACPI_HANDLE acpiobj)15785cff7825Smh acpica_tag_devinfo(dev_info_t *dip, ACPI_HANDLE acpiobj)
15797c478bd9Sstevel@tonic-gate {
15805cff7825Smh ACPI_STATUS status;
15815cff7825Smh ACPI_BUFFER rb;
15827c478bd9Sstevel@tonic-gate
15835cff7825Smh /*
15845cff7825Smh * Tag the devinfo node with the ACPI name
15855cff7825Smh */
15865cff7825Smh rb.Pointer = NULL;
15875cff7825Smh rb.Length = ACPI_ALLOCATE_BUFFER;
1588fa96bd91SMichael Corcoran status = AcpiGetName(acpiobj, ACPI_FULL_PATHNAME, &rb);
1589fa96bd91SMichael Corcoran if (ACPI_FAILURE(status)) {
1590fa96bd91SMichael Corcoran cmn_err(CE_WARN, "acpica: could not get ACPI path!");
1591fa96bd91SMichael Corcoran } else {
15925cff7825Smh (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
15935cff7825Smh "acpi-namespace", (char *)rb.Pointer);
15945cff7825Smh AcpiOsFree(rb.Pointer);
15957c478bd9Sstevel@tonic-gate
1596fa96bd91SMichael Corcoran /*
1597fa96bd91SMichael Corcoran * Tag the ACPI node with the dip
1598fa96bd91SMichael Corcoran */
1599fa96bd91SMichael Corcoran status = acpica_set_devinfo(acpiobj, dip);
1600fa96bd91SMichael Corcoran ASSERT(ACPI_SUCCESS(status));
16015cff7825Smh }
16025cff7825Smh
1603fa96bd91SMichael Corcoran return (status);
1604fa96bd91SMichael Corcoran }
16055cff7825Smh
1606fa96bd91SMichael Corcoran /*
1607fa96bd91SMichael Corcoran * Destroy linkage between devinfo nodes and ACPI nodes
1608fa96bd91SMichael Corcoran */
1609fa96bd91SMichael Corcoran ACPI_STATUS
acpica_untag_devinfo(dev_info_t * dip,ACPI_HANDLE acpiobj)1610fa96bd91SMichael Corcoran acpica_untag_devinfo(dev_info_t *dip, ACPI_HANDLE acpiobj)
1611fa96bd91SMichael Corcoran {
1612fa96bd91SMichael Corcoran (void) acpica_unset_devinfo(acpiobj);
1613fa96bd91SMichael Corcoran (void) ndi_prop_remove(DDI_DEV_T_NONE, dip, "acpi-namespace");
16147c478bd9Sstevel@tonic-gate
1615fa96bd91SMichael Corcoran return (AE_OK);
16167c478bd9Sstevel@tonic-gate }
16177c478bd9Sstevel@tonic-gate
16187c478bd9Sstevel@tonic-gate /*
1619495256c0Smh * Return the ACPI device node matching the CPU dev_info node.
16207c478bd9Sstevel@tonic-gate */
16215cff7825Smh ACPI_STATUS
acpica_get_handle_cpu(int cpu_id,ACPI_HANDLE * rh)16220e751525SEric Saxe acpica_get_handle_cpu(int cpu_id, ACPI_HANDLE *rh)
16237c478bd9Sstevel@tonic-gate {
1624fa96bd91SMichael Corcoran int i;
1625fa96bd91SMichael Corcoran
16265cff7825Smh /*
16275cff7825Smh * if cpu_map itself is NULL, we're a uppc system and
16285cff7825Smh * acpica_build_processor_map() hasn't been called yet.
16295cff7825Smh * So call it here
16305cff7825Smh */
16315cff7825Smh if (cpu_map == NULL) {
16325cff7825Smh (void) acpica_build_processor_map();
16335cff7825Smh if (cpu_map == NULL)
16345cff7825Smh return (AE_ERROR);
16357c478bd9Sstevel@tonic-gate }
16367c478bd9Sstevel@tonic-gate
1637fa96bd91SMichael Corcoran if (cpu_id < 0) {
16385cff7825Smh return (AE_ERROR);
1639fa96bd91SMichael Corcoran }
16405cff7825Smh
1641fa96bd91SMichael Corcoran /*
1642fa96bd91SMichael Corcoran * search object with cpuid in cpu_map
1643fa96bd91SMichael Corcoran */
1644fa96bd91SMichael Corcoran mutex_enter(&cpu_map_lock);
1645fa96bd91SMichael Corcoran for (i = 0; i < cpu_map_count; i++) {
1646fa96bd91SMichael Corcoran if (cpu_map[i]->cpu_id == cpu_id) {
1647fa96bd91SMichael Corcoran break;
1648fa96bd91SMichael Corcoran }
1649fa96bd91SMichael Corcoran }
1650b72d5b75SMichael Corcoran if (i < cpu_map_count && (cpu_map[i]->obj != NULL)) {
1651a3114836SGerry Liu *rh = cpu_map[i]->obj;
1652fa96bd91SMichael Corcoran mutex_exit(&cpu_map_lock);
1653b72d5b75SMichael Corcoran return (AE_OK);
1654b72d5b75SMichael Corcoran }
1655b72d5b75SMichael Corcoran
1656b72d5b75SMichael Corcoran /* Handle special case for uppc-only systems. */
1657b72d5b75SMichael Corcoran if (cpu_map_called == 0) {
1658b72d5b75SMichael Corcoran uint32_t apicid = cpuid_get_apicid(CPU);
1659b72d5b75SMichael Corcoran if (apicid != UINT32_MAX) {
1660b72d5b75SMichael Corcoran for (i = 0; i < cpu_map_count; i++) {
1661b72d5b75SMichael Corcoran if (cpu_map[i]->apic_id == apicid) {
1662b72d5b75SMichael Corcoran break;
1663b72d5b75SMichael Corcoran }
1664b72d5b75SMichael Corcoran }
1665b72d5b75SMichael Corcoran if (i < cpu_map_count && (cpu_map[i]->obj != NULL)) {
1666a3114836SGerry Liu *rh = cpu_map[i]->obj;
1667b72d5b75SMichael Corcoran mutex_exit(&cpu_map_lock);
1668b72d5b75SMichael Corcoran return (AE_OK);
1669b72d5b75SMichael Corcoran }
1670b72d5b75SMichael Corcoran }
1671fa96bd91SMichael Corcoran }
1672fa96bd91SMichael Corcoran mutex_exit(&cpu_map_lock);
1673fa96bd91SMichael Corcoran
1674b72d5b75SMichael Corcoran return (AE_ERROR);
16755cff7825Smh }
16765cff7825Smh
16775cff7825Smh /*
16785cff7825Smh * Determine if this object is a processor
16795cff7825Smh */
16805cff7825Smh static ACPI_STATUS
acpica_probe_processor(ACPI_HANDLE obj,UINT32 level,void * ctx,void ** rv)16815cff7825Smh acpica_probe_processor(ACPI_HANDLE obj, UINT32 level, void *ctx, void **rv)
16825cff7825Smh {
16835cff7825Smh ACPI_STATUS status;
16845cff7825Smh ACPI_OBJECT_TYPE objtype;
16854b1ce0a9SDana Myers unsigned long acpi_id;
16865cff7825Smh ACPI_BUFFER rb;
168757190917SDana Myers ACPI_DEVICE_INFO *di;
16885cff7825Smh
16895cff7825Smh if (AcpiGetType(obj, &objtype) != AE_OK)
16905cff7825Smh return (AE_OK);
16915cff7825Smh
16925cff7825Smh if (objtype == ACPI_TYPE_PROCESSOR) {
16935cff7825Smh /* process a Processor */
16947c478bd9Sstevel@tonic-gate rb.Pointer = NULL;
16957c478bd9Sstevel@tonic-gate rb.Length = ACPI_ALLOCATE_BUFFER;
1696db2bae30SDana Myers status = AcpiEvaluateObjectTyped(obj, NULL, NULL, &rb,
1697db2bae30SDana Myers ACPI_TYPE_PROCESSOR);
16985cff7825Smh if (status != AE_OK) {
16994b1ce0a9SDana Myers cmn_err(CE_WARN, "!acpica: error probing Processor");
17005cff7825Smh return (status);
17017c478bd9Sstevel@tonic-gate }
17025cff7825Smh acpi_id = ((ACPI_OBJECT *)rb.Pointer)->Processor.ProcId;
17035cff7825Smh AcpiOsFree(rb.Pointer);
17045cff7825Smh } else if (objtype == ACPI_TYPE_DEVICE) {
17055cff7825Smh /* process a processor Device */
170657190917SDana Myers status = AcpiGetObjectInfo(obj, &di);
1707b6917abeSmishra if (status != AE_OK) {
1708b6917abeSmishra cmn_err(CE_WARN,
17094b1ce0a9SDana Myers "!acpica: error probing Processor Device\n");
1710b6917abeSmishra return (status);
1711b6917abeSmishra }
17124b1ce0a9SDana Myers
171357190917SDana Myers if (!(di->Valid & ACPI_VALID_UID) ||
171457190917SDana Myers ddi_strtoul(di->UniqueId.String, NULL, 10, &acpi_id) != 0) {
171557190917SDana Myers ACPI_FREE(di);
17164b1ce0a9SDana Myers cmn_err(CE_WARN,
17174b1ce0a9SDana Myers "!acpica: error probing Processor Device _UID\n");
17184b1ce0a9SDana Myers return (AE_ERROR);
17194b1ce0a9SDana Myers }
172057190917SDana Myers ACPI_FREE(di);
17217c478bd9Sstevel@tonic-gate }
1722fa96bd91SMichael Corcoran (void) acpica_add_processor_to_map(acpi_id, obj, UINT32_MAX);
17235cff7825Smh
17245cff7825Smh return (AE_OK);
17257c478bd9Sstevel@tonic-gate }
17267c478bd9Sstevel@tonic-gate
1727432cf5a9SDana Myers void
scan_d2a_map(void)17285cff7825Smh scan_d2a_map(void)
17297c478bd9Sstevel@tonic-gate {
17307c478bd9Sstevel@tonic-gate dev_info_t *dip, *cdip;
17317c478bd9Sstevel@tonic-gate ACPI_HANDLE acpiobj;
17327c478bd9Sstevel@tonic-gate char *device_type_prop;
17337c478bd9Sstevel@tonic-gate int bus;
17347c478bd9Sstevel@tonic-gate static int map_error = 0;
17357c478bd9Sstevel@tonic-gate
1736432cf5a9SDana Myers if (map_error || (d2a_done != 0))
17377c478bd9Sstevel@tonic-gate return;
17387c478bd9Sstevel@tonic-gate
17395cff7825Smh scanning_d2a_map = 1;
17407c478bd9Sstevel@tonic-gate
17417c478bd9Sstevel@tonic-gate /*
17427c478bd9Sstevel@tonic-gate * Find all child-of-root PCI buses, and find their corresponding
17437c478bd9Sstevel@tonic-gate * ACPI child-of-root PCI nodes. For each one, add to the
17447c478bd9Sstevel@tonic-gate * d2a table.
17457c478bd9Sstevel@tonic-gate */
17467c478bd9Sstevel@tonic-gate
17477c478bd9Sstevel@tonic-gate for (dip = ddi_get_child(ddi_root_node());
17487c478bd9Sstevel@tonic-gate dip != NULL;
17497c478bd9Sstevel@tonic-gate dip = ddi_get_next_sibling(dip)) {
17507c478bd9Sstevel@tonic-gate
17517c478bd9Sstevel@tonic-gate /* prune non-PCI nodes */
1752fa96bd91SMichael Corcoran if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
1753fa96bd91SMichael Corcoran DDI_PROP_DONTPASS,
17547c478bd9Sstevel@tonic-gate "device_type", &device_type_prop) != DDI_PROP_SUCCESS)
17557c478bd9Sstevel@tonic-gate continue;
17567c478bd9Sstevel@tonic-gate
175770025d76Sjohnny if ((strcmp("pci", device_type_prop) != 0) &&
175870025d76Sjohnny (strcmp("pciex", device_type_prop) != 0)) {
17597c478bd9Sstevel@tonic-gate ddi_prop_free(device_type_prop);
17607c478bd9Sstevel@tonic-gate continue;
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate
17637c478bd9Sstevel@tonic-gate ddi_prop_free(device_type_prop);
17647c478bd9Sstevel@tonic-gate
17657c478bd9Sstevel@tonic-gate /*
17667c478bd9Sstevel@tonic-gate * To get bus number of dip, get first child and get its
17677c478bd9Sstevel@tonic-gate * bus number. If NULL, just continue, because we don't
17687c478bd9Sstevel@tonic-gate * care about bus nodes with no children anyway.
17697c478bd9Sstevel@tonic-gate */
17707c478bd9Sstevel@tonic-gate if ((cdip = ddi_get_child(dip)) == NULL)
17717c478bd9Sstevel@tonic-gate continue;
17727c478bd9Sstevel@tonic-gate
17737c478bd9Sstevel@tonic-gate if (acpica_get_bdf(cdip, &bus, NULL, NULL) < 0) {
17747c478bd9Sstevel@tonic-gate #ifdef D2ADEBUG
17757c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "Can't get bus number of PCI child?");
17767c478bd9Sstevel@tonic-gate #endif
17777c478bd9Sstevel@tonic-gate map_error = 1;
17785cff7825Smh scanning_d2a_map = 0;
17795cff7825Smh d2a_done = 1;
17807c478bd9Sstevel@tonic-gate return;
17817c478bd9Sstevel@tonic-gate }
17827c478bd9Sstevel@tonic-gate
17837c478bd9Sstevel@tonic-gate if (acpica_find_pcibus(bus, &acpiobj) == AE_ERROR) {
17847c478bd9Sstevel@tonic-gate #ifdef D2ADEBUG
17857c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "No ACPI bus obj for bus %d?\n", bus);
17867c478bd9Sstevel@tonic-gate #endif
17877c478bd9Sstevel@tonic-gate map_error = 1;
17887c478bd9Sstevel@tonic-gate continue;
17897c478bd9Sstevel@tonic-gate }
17907c478bd9Sstevel@tonic-gate
17915cff7825Smh acpica_tag_devinfo(dip, acpiobj);
17927c478bd9Sstevel@tonic-gate
17935cff7825Smh /* call recursively to enumerate subtrees */
17945cff7825Smh scan_d2a_subtree(dip, acpiobj, bus);
17957c478bd9Sstevel@tonic-gate }
17965cff7825Smh
17975cff7825Smh scanning_d2a_map = 0;
17985cff7825Smh d2a_done = 1;
17997c478bd9Sstevel@tonic-gate }
18007c478bd9Sstevel@tonic-gate
18017c478bd9Sstevel@tonic-gate /*
18027c478bd9Sstevel@tonic-gate * For all acpi child devices of acpiobj, find their matching
18037c478bd9Sstevel@tonic-gate * dip under "dip" argument. (matching means "matches dev/fn").
18047c478bd9Sstevel@tonic-gate * bus is assumed to already be a match from caller, and is
18057c478bd9Sstevel@tonic-gate * used here only to record in the d2a entry. Recurse if necessary.
18067c478bd9Sstevel@tonic-gate */
18077c478bd9Sstevel@tonic-gate static void
scan_d2a_subtree(dev_info_t * dip,ACPI_HANDLE acpiobj,int bus)18085cff7825Smh scan_d2a_subtree(dev_info_t *dip, ACPI_HANDLE acpiobj, int bus)
18097c478bd9Sstevel@tonic-gate {
18107c478bd9Sstevel@tonic-gate int acpi_devfn, hid;
18117c478bd9Sstevel@tonic-gate ACPI_HANDLE acld;
18127c478bd9Sstevel@tonic-gate dev_info_t *dcld;
18137c478bd9Sstevel@tonic-gate int dcld_b, dcld_d, dcld_f;
18147c478bd9Sstevel@tonic-gate int dev, func;
18155cff7825Smh char *device_type_prop;
18167c478bd9Sstevel@tonic-gate
18177c478bd9Sstevel@tonic-gate acld = NULL;
1818186507a7Smyers while (AcpiGetNextObject(ACPI_TYPE_DEVICE, acpiobj, acld, &acld)
18197c478bd9Sstevel@tonic-gate == AE_OK) {
18207c478bd9Sstevel@tonic-gate /* get the dev/func we're looking for in the devinfo tree */
18217c478bd9Sstevel@tonic-gate if (acpica_eval_int(acld, "_ADR", &acpi_devfn) != AE_OK)
18227c478bd9Sstevel@tonic-gate continue;
18237c478bd9Sstevel@tonic-gate dev = (acpi_devfn >> 16) & 0xFFFF;
18247c478bd9Sstevel@tonic-gate func = acpi_devfn & 0xFFFF;
18257c478bd9Sstevel@tonic-gate
18267c478bd9Sstevel@tonic-gate /* look through all the immediate children of dip */
18277c478bd9Sstevel@tonic-gate for (dcld = ddi_get_child(dip); dcld != NULL;
18287c478bd9Sstevel@tonic-gate dcld = ddi_get_next_sibling(dcld)) {
18297c478bd9Sstevel@tonic-gate if (acpica_get_bdf(dcld, &dcld_b, &dcld_d, &dcld_f) < 0)
18307c478bd9Sstevel@tonic-gate continue;
18317c478bd9Sstevel@tonic-gate
18327c478bd9Sstevel@tonic-gate /* dev must match; function must match or wildcard */
18337c478bd9Sstevel@tonic-gate if (dcld_d != dev ||
18347c478bd9Sstevel@tonic-gate (func != 0xFFFF && func != dcld_f))
18357c478bd9Sstevel@tonic-gate continue;
18367c478bd9Sstevel@tonic-gate bus = dcld_b;
18377c478bd9Sstevel@tonic-gate
18387c478bd9Sstevel@tonic-gate /* found a match, record it */
18395cff7825Smh acpica_tag_devinfo(dcld, acld);
18405cff7825Smh
18415cff7825Smh /* if we find a bridge, recurse from here */
1842fa96bd91SMichael Corcoran if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dcld,
1843fa96bd91SMichael Corcoran DDI_PROP_DONTPASS, "device_type",
1844fa96bd91SMichael Corcoran &device_type_prop) == DDI_PROP_SUCCESS) {
18455cff7825Smh if ((strcmp("pci", device_type_prop) == 0) ||
18465cff7825Smh (strcmp("pciex", device_type_prop) == 0))
18475cff7825Smh scan_d2a_subtree(dcld, acld, bus);
18485cff7825Smh ddi_prop_free(device_type_prop);
18495cff7825Smh }
18507c478bd9Sstevel@tonic-gate
18517c478bd9Sstevel@tonic-gate /* done finding a match, so break now */
18527c478bd9Sstevel@tonic-gate break;
18537c478bd9Sstevel@tonic-gate }
18548e56767dSsmall }
18557c478bd9Sstevel@tonic-gate }
18567c478bd9Sstevel@tonic-gate
18577c478bd9Sstevel@tonic-gate /*
18587c478bd9Sstevel@tonic-gate * Return bus/dev/fn for PCI dip (note: not the parent "pci" node).
18597c478bd9Sstevel@tonic-gate */
18607c478bd9Sstevel@tonic-gate int
acpica_get_bdf(dev_info_t * dip,int * bus,int * device,int * func)18617c478bd9Sstevel@tonic-gate acpica_get_bdf(dev_info_t *dip, int *bus, int *device, int *func)
18627c478bd9Sstevel@tonic-gate {
18637c478bd9Sstevel@tonic-gate pci_regspec_t *pci_rp;
18647c478bd9Sstevel@tonic-gate int len;
18657c478bd9Sstevel@tonic-gate
18667c478bd9Sstevel@tonic-gate if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
18677c478bd9Sstevel@tonic-gate "reg", (int **)&pci_rp, (uint_t *)&len) != DDI_SUCCESS)
18687c478bd9Sstevel@tonic-gate return (-1);
18697c478bd9Sstevel@tonic-gate
18707c478bd9Sstevel@tonic-gate if (len < (sizeof (pci_regspec_t) / sizeof (int))) {
18717c478bd9Sstevel@tonic-gate ddi_prop_free(pci_rp);
18727c478bd9Sstevel@tonic-gate return (-1);
18737c478bd9Sstevel@tonic-gate }
18747c478bd9Sstevel@tonic-gate if (bus != NULL)
18757c478bd9Sstevel@tonic-gate *bus = (int)PCI_REG_BUS_G(pci_rp->pci_phys_hi);
18767c478bd9Sstevel@tonic-gate if (device != NULL)
18777c478bd9Sstevel@tonic-gate *device = (int)PCI_REG_DEV_G(pci_rp->pci_phys_hi);
18787c478bd9Sstevel@tonic-gate if (func != NULL)
18797c478bd9Sstevel@tonic-gate *func = (int)PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
18807c478bd9Sstevel@tonic-gate ddi_prop_free(pci_rp);
18817c478bd9Sstevel@tonic-gate return (0);
18827c478bd9Sstevel@tonic-gate }
18835cff7825Smh
18845cff7825Smh /*
18855cff7825Smh * Return the ACPI device node matching this dev_info node, if it
18865cff7825Smh * exists in the ACPI tree.
18875cff7825Smh */
18885cff7825Smh ACPI_STATUS
acpica_get_handle(dev_info_t * dip,ACPI_HANDLE * rh)18895cff7825Smh acpica_get_handle(dev_info_t *dip, ACPI_HANDLE *rh)
18905cff7825Smh {
18915cff7825Smh ACPI_STATUS status;
18925cff7825Smh char *acpiname;
18935cff7825Smh
1894149e87b3SDana Myers #ifdef DEBUG
1895149e87b3SDana Myers if (d2a_done == 0)
1896149e87b3SDana Myers cmn_err(CE_WARN, "!acpica_get_handle:"
1897149e87b3SDana Myers " no ACPI mapping for %s", ddi_node_name(dip));
1898149e87b3SDana Myers #endif
18995cff7825Smh
19005cff7825Smh if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
19015cff7825Smh "acpi-namespace", &acpiname) != DDI_PROP_SUCCESS) {
19020e751525SEric Saxe return (AE_ERROR);
19035cff7825Smh }
19045cff7825Smh
19055cff7825Smh status = AcpiGetHandle(NULL, acpiname, rh);
19065cff7825Smh ddi_prop_free((void *)acpiname);
19075cff7825Smh return (status);
19085cff7825Smh }
19095cff7825Smh
19105cff7825Smh
19115cff7825Smh
19125cff7825Smh /*
19135cff7825Smh * Manage OS data attachment to ACPI nodes
19145cff7825Smh */
19155cff7825Smh
19165cff7825Smh /*
19175cff7825Smh * Return the (dev_info_t *) associated with the ACPI node.
19185cff7825Smh */
19195cff7825Smh ACPI_STATUS
acpica_get_devinfo(ACPI_HANDLE obj,dev_info_t ** dipp)19205cff7825Smh acpica_get_devinfo(ACPI_HANDLE obj, dev_info_t **dipp)
19215cff7825Smh {
19225cff7825Smh ACPI_STATUS status;
19235cff7825Smh void *ptr;
19245cff7825Smh
19255cff7825Smh status = AcpiGetData(obj, acpica_devinfo_handler, &ptr);
19265cff7825Smh if (status == AE_OK)
19275cff7825Smh *dipp = (dev_info_t *)ptr;
19285cff7825Smh
19295cff7825Smh return (status);
19305cff7825Smh }
19315cff7825Smh
19325cff7825Smh /*
19335cff7825Smh * Set the dev_info_t associated with the ACPI node.
19345cff7825Smh */
19355cff7825Smh static ACPI_STATUS
acpica_set_devinfo(ACPI_HANDLE obj,dev_info_t * dip)19365cff7825Smh acpica_set_devinfo(ACPI_HANDLE obj, dev_info_t *dip)
19375cff7825Smh {
19385cff7825Smh ACPI_STATUS status;
19395cff7825Smh
19405cff7825Smh status = AcpiAttachData(obj, acpica_devinfo_handler, (void *)dip);
19415cff7825Smh return (status);
19425cff7825Smh }
19435cff7825Smh
19445cff7825Smh /*
1945fa96bd91SMichael Corcoran * Unset the dev_info_t associated with the ACPI node.
19465cff7825Smh */
1947fa96bd91SMichael Corcoran static ACPI_STATUS
acpica_unset_devinfo(ACPI_HANDLE obj)1948fa96bd91SMichael Corcoran acpica_unset_devinfo(ACPI_HANDLE obj)
19495cff7825Smh {
1950fa96bd91SMichael Corcoran return (AcpiDetachData(obj, acpica_devinfo_handler));
19515cff7825Smh }
19525cff7825Smh
19535cff7825Smh /*
19545cff7825Smh *
19555cff7825Smh */
19565cff7825Smh void
acpica_devinfo_handler(ACPI_HANDLE obj,void * data)195757190917SDana Myers acpica_devinfo_handler(ACPI_HANDLE obj, void *data)
19585cff7825Smh {
195957190917SDana Myers /* no-op */
19605cff7825Smh }
19615cff7825Smh
1962fa96bd91SMichael Corcoran ACPI_STATUS
acpica_build_processor_map(void)1963fa96bd91SMichael Corcoran acpica_build_processor_map(void)
19645cff7825Smh {
19655cff7825Smh ACPI_STATUS status;
19665cff7825Smh void *rv;
19675cff7825Smh
19685cff7825Smh /*
19695cff7825Smh * shouldn't be called more than once anyway
19705cff7825Smh */
19715cff7825Smh if (cpu_map_built)
1972fa96bd91SMichael Corcoran return (AE_OK);
1973fa96bd91SMichael Corcoran
1974fa96bd91SMichael Corcoran /*
1975fa96bd91SMichael Corcoran * ACPI device configuration driver has built mapping information
1976fa96bd91SMichael Corcoran * among processor id and object handle, no need to probe again.
1977fa96bd91SMichael Corcoran */
1978fa96bd91SMichael Corcoran if (acpica_get_devcfg_feature(ACPI_DEVCFG_CPU)) {
1979fa96bd91SMichael Corcoran cpu_map_built = 1;
1980fa96bd91SMichael Corcoran return (AE_OK);
1981fa96bd91SMichael Corcoran }
19825cff7825Smh
19835cff7825Smh /*
19845cff7825Smh * Look for Processor objects
19855cff7825Smh */
19865cff7825Smh status = AcpiWalkNamespace(ACPI_TYPE_PROCESSOR,
19875cff7825Smh ACPI_ROOT_OBJECT,
19885cff7825Smh 4,
19895cff7825Smh acpica_probe_processor,
19905cff7825Smh NULL,
199157190917SDana Myers NULL,
19925cff7825Smh &rv);
19935cff7825Smh ASSERT(status == AE_OK);
19945cff7825Smh
19955cff7825Smh /*
19965cff7825Smh * Look for processor Device objects
19975cff7825Smh */
19985cff7825Smh status = AcpiGetDevices("ACPI0007",
19995cff7825Smh acpica_probe_processor,
20005cff7825Smh NULL,
20015cff7825Smh &rv);
20025cff7825Smh ASSERT(status == AE_OK);
20035cff7825Smh cpu_map_built = 1;
2004fa96bd91SMichael Corcoran
2005fa96bd91SMichael Corcoran return (status);
2006fa96bd91SMichael Corcoran }
2007fa96bd91SMichael Corcoran
2008fa96bd91SMichael Corcoran /*
2009fa96bd91SMichael Corcoran * Grow cpu map table on demand.
2010fa96bd91SMichael Corcoran */
2011fa96bd91SMichael Corcoran static void
acpica_grow_cpu_map(void)2012fa96bd91SMichael Corcoran acpica_grow_cpu_map(void)
2013fa96bd91SMichael Corcoran {
2014fa96bd91SMichael Corcoran if (cpu_map_count == cpu_map_count_max) {
2015fa96bd91SMichael Corcoran size_t sz;
2016fa96bd91SMichael Corcoran struct cpu_map_item **new_map;
2017fa96bd91SMichael Corcoran
2018fa96bd91SMichael Corcoran ASSERT(cpu_map_count_max < INT_MAX / 2);
2019fa96bd91SMichael Corcoran cpu_map_count_max += max_ncpus;
2020fa96bd91SMichael Corcoran new_map = kmem_zalloc(sizeof (cpu_map[0]) * cpu_map_count_max,
2021fa96bd91SMichael Corcoran KM_SLEEP);
2022fa96bd91SMichael Corcoran if (cpu_map_count != 0) {
2023fa96bd91SMichael Corcoran ASSERT(cpu_map != NULL);
2024fa96bd91SMichael Corcoran sz = sizeof (cpu_map[0]) * cpu_map_count;
2025fa96bd91SMichael Corcoran kcopy(cpu_map, new_map, sz);
2026fa96bd91SMichael Corcoran kmem_free(cpu_map, sz);
2027fa96bd91SMichael Corcoran }
2028fa96bd91SMichael Corcoran cpu_map = new_map;
2029fa96bd91SMichael Corcoran }
2030fa96bd91SMichael Corcoran }
2031fa96bd91SMichael Corcoran
2032fa96bd91SMichael Corcoran /*
2033fa96bd91SMichael Corcoran * Maintain mapping information among (cpu id, ACPI processor id, APIC id,
2034fa96bd91SMichael Corcoran * ACPI handle). The mapping table will be setup in two steps:
2035fa96bd91SMichael Corcoran * 1) acpica_add_processor_to_map() builds mapping among APIC id, ACPI
2036fa96bd91SMichael Corcoran * processor id and ACPI object handle.
2037fa96bd91SMichael Corcoran * 2) acpica_map_cpu() builds mapping among cpu id and ACPI processor id.
2038b72d5b75SMichael Corcoran * On systems with which have ACPI device configuration for CPUs enabled,
2039b72d5b75SMichael Corcoran * acpica_map_cpu() will be called after acpica_add_processor_to_map(),
2040b72d5b75SMichael Corcoran * otherwise acpica_map_cpu() will be called before
2041b72d5b75SMichael Corcoran * acpica_add_processor_to_map().
2042fa96bd91SMichael Corcoran */
2043fa96bd91SMichael Corcoran ACPI_STATUS
acpica_add_processor_to_map(UINT32 acpi_id,ACPI_HANDLE obj,UINT32 apic_id)2044fa96bd91SMichael Corcoran acpica_add_processor_to_map(UINT32 acpi_id, ACPI_HANDLE obj, UINT32 apic_id)
2045fa96bd91SMichael Corcoran {
2046fa96bd91SMichael Corcoran int i;
2047fa96bd91SMichael Corcoran ACPI_STATUS rc = AE_OK;
2048fa96bd91SMichael Corcoran struct cpu_map_item *item = NULL;
2049fa96bd91SMichael Corcoran
2050fa96bd91SMichael Corcoran ASSERT(obj != NULL);
2051fa96bd91SMichael Corcoran if (obj == NULL) {
2052fa96bd91SMichael Corcoran return (AE_ERROR);
2053fa96bd91SMichael Corcoran }
2054fa96bd91SMichael Corcoran
2055fa96bd91SMichael Corcoran mutex_enter(&cpu_map_lock);
2056fa96bd91SMichael Corcoran
2057fa96bd91SMichael Corcoran /*
2058fa96bd91SMichael Corcoran * Special case for uppc
2059fa96bd91SMichael Corcoran * If we're a uppc system and ACPI device configuration for CPU has
2060fa96bd91SMichael Corcoran * been disabled, there won't be a CPU map yet because uppc psm doesn't
2061fa96bd91SMichael Corcoran * call acpica_map_cpu(). So create one and use the passed-in processor
2062fa96bd91SMichael Corcoran * as CPU 0
2063b72d5b75SMichael Corcoran * Assumption: the first CPU returned by
2064b72d5b75SMichael Corcoran * AcpiGetDevices/AcpiWalkNamespace will be the BSP.
2065b72d5b75SMichael Corcoran * Unfortunately there appears to be no good way to ASSERT this.
2066fa96bd91SMichael Corcoran */
2067fa96bd91SMichael Corcoran if (cpu_map == NULL &&
2068fa96bd91SMichael Corcoran !acpica_get_devcfg_feature(ACPI_DEVCFG_CPU)) {
2069fa96bd91SMichael Corcoran acpica_grow_cpu_map();
2070fa96bd91SMichael Corcoran ASSERT(cpu_map != NULL);
2071fa96bd91SMichael Corcoran item = kmem_zalloc(sizeof (*item), KM_SLEEP);
2072fa96bd91SMichael Corcoran item->cpu_id = 0;
2073fa96bd91SMichael Corcoran item->proc_id = acpi_id;
2074fa96bd91SMichael Corcoran item->apic_id = apic_id;
2075fa96bd91SMichael Corcoran item->obj = obj;
2076fa96bd91SMichael Corcoran cpu_map[0] = item;
2077fa96bd91SMichael Corcoran cpu_map_count = 1;
2078fa96bd91SMichael Corcoran mutex_exit(&cpu_map_lock);
2079fa96bd91SMichael Corcoran return (AE_OK);
2080fa96bd91SMichael Corcoran }
2081fa96bd91SMichael Corcoran
2082fa96bd91SMichael Corcoran for (i = 0; i < cpu_map_count; i++) {
2083fa96bd91SMichael Corcoran if (cpu_map[i]->obj == obj) {
2084fa96bd91SMichael Corcoran rc = AE_ALREADY_EXISTS;
2085fa96bd91SMichael Corcoran break;
2086fa96bd91SMichael Corcoran } else if (cpu_map[i]->proc_id == acpi_id) {
2087fa96bd91SMichael Corcoran ASSERT(item == NULL);
2088fa96bd91SMichael Corcoran item = cpu_map[i];
2089fa96bd91SMichael Corcoran }
2090fa96bd91SMichael Corcoran }
2091fa96bd91SMichael Corcoran
2092fa96bd91SMichael Corcoran if (rc == AE_OK) {
2093fa96bd91SMichael Corcoran if (item != NULL) {
2094fa96bd91SMichael Corcoran /*
2095fa96bd91SMichael Corcoran * ACPI alias objects may cause more than one objects
2096fa96bd91SMichael Corcoran * with the same ACPI processor id, only remember the
2097fa96bd91SMichael Corcoran * the first object encountered.
2098fa96bd91SMichael Corcoran */
2099fa96bd91SMichael Corcoran if (item->obj == NULL) {
2100fa96bd91SMichael Corcoran item->obj = obj;
2101fa96bd91SMichael Corcoran item->apic_id = apic_id;
2102fa96bd91SMichael Corcoran } else {
2103fa96bd91SMichael Corcoran rc = AE_ALREADY_EXISTS;
2104fa96bd91SMichael Corcoran }
2105fa96bd91SMichael Corcoran } else if (cpu_map_count >= INT_MAX / 2) {
2106fa96bd91SMichael Corcoran rc = AE_NO_MEMORY;
2107fa96bd91SMichael Corcoran } else {
2108fa96bd91SMichael Corcoran acpica_grow_cpu_map();
2109fa96bd91SMichael Corcoran ASSERT(cpu_map != NULL);
2110fa96bd91SMichael Corcoran ASSERT(cpu_map_count < cpu_map_count_max);
2111fa96bd91SMichael Corcoran item = kmem_zalloc(sizeof (*item), KM_SLEEP);
2112fa96bd91SMichael Corcoran item->cpu_id = -1;
2113fa96bd91SMichael Corcoran item->proc_id = acpi_id;
2114fa96bd91SMichael Corcoran item->apic_id = apic_id;
2115fa96bd91SMichael Corcoran item->obj = obj;
2116fa96bd91SMichael Corcoran cpu_map[cpu_map_count] = item;
2117fa96bd91SMichael Corcoran cpu_map_count++;
2118fa96bd91SMichael Corcoran }
2119fa96bd91SMichael Corcoran }
2120fa96bd91SMichael Corcoran
2121fa96bd91SMichael Corcoran mutex_exit(&cpu_map_lock);
2122fa96bd91SMichael Corcoran
2123fa96bd91SMichael Corcoran return (rc);
2124fa96bd91SMichael Corcoran }
2125fa96bd91SMichael Corcoran
2126fa96bd91SMichael Corcoran ACPI_STATUS
acpica_remove_processor_from_map(UINT32 acpi_id)2127fa96bd91SMichael Corcoran acpica_remove_processor_from_map(UINT32 acpi_id)
2128fa96bd91SMichael Corcoran {
2129fa96bd91SMichael Corcoran int i;
2130fa96bd91SMichael Corcoran ACPI_STATUS rc = AE_NOT_EXIST;
2131fa96bd91SMichael Corcoran
2132fa96bd91SMichael Corcoran mutex_enter(&cpu_map_lock);
2133fa96bd91SMichael Corcoran for (i = 0; i < cpu_map_count; i++) {
2134fa96bd91SMichael Corcoran if (cpu_map[i]->proc_id != acpi_id) {
2135fa96bd91SMichael Corcoran continue;
2136fa96bd91SMichael Corcoran }
2137fa96bd91SMichael Corcoran cpu_map[i]->obj = NULL;
2138fa96bd91SMichael Corcoran /* Free item if no more reference to it. */
2139fa96bd91SMichael Corcoran if (cpu_map[i]->cpu_id == -1) {
2140fa96bd91SMichael Corcoran kmem_free(cpu_map[i], sizeof (struct cpu_map_item));
2141fa96bd91SMichael Corcoran cpu_map[i] = NULL;
2142fa96bd91SMichael Corcoran cpu_map_count--;
2143fa96bd91SMichael Corcoran if (i != cpu_map_count) {
2144fa96bd91SMichael Corcoran cpu_map[i] = cpu_map[cpu_map_count];
2145fa96bd91SMichael Corcoran cpu_map[cpu_map_count] = NULL;
2146fa96bd91SMichael Corcoran }
2147fa96bd91SMichael Corcoran }
2148fa96bd91SMichael Corcoran rc = AE_OK;
2149fa96bd91SMichael Corcoran break;
2150fa96bd91SMichael Corcoran }
2151fa96bd91SMichael Corcoran mutex_exit(&cpu_map_lock);
2152fa96bd91SMichael Corcoran
2153fa96bd91SMichael Corcoran return (rc);
2154fa96bd91SMichael Corcoran }
2155fa96bd91SMichael Corcoran
2156fa96bd91SMichael Corcoran ACPI_STATUS
acpica_map_cpu(processorid_t cpuid,UINT32 acpi_id)2157fa96bd91SMichael Corcoran acpica_map_cpu(processorid_t cpuid, UINT32 acpi_id)
2158fa96bd91SMichael Corcoran {
2159fa96bd91SMichael Corcoran int i;
2160fa96bd91SMichael Corcoran ACPI_STATUS rc = AE_OK;
2161fa96bd91SMichael Corcoran struct cpu_map_item *item = NULL;
2162fa96bd91SMichael Corcoran
2163fa96bd91SMichael Corcoran ASSERT(cpuid != -1);
2164fa96bd91SMichael Corcoran if (cpuid == -1) {
2165fa96bd91SMichael Corcoran return (AE_ERROR);
2166fa96bd91SMichael Corcoran }
2167fa96bd91SMichael Corcoran
2168fa96bd91SMichael Corcoran mutex_enter(&cpu_map_lock);
2169b72d5b75SMichael Corcoran cpu_map_called = 1;
2170fa96bd91SMichael Corcoran for (i = 0; i < cpu_map_count; i++) {
2171fa96bd91SMichael Corcoran if (cpu_map[i]->cpu_id == cpuid) {
2172fa96bd91SMichael Corcoran rc = AE_ALREADY_EXISTS;
2173fa96bd91SMichael Corcoran break;
2174fa96bd91SMichael Corcoran } else if (cpu_map[i]->proc_id == acpi_id) {
2175fa96bd91SMichael Corcoran ASSERT(item == NULL);
2176fa96bd91SMichael Corcoran item = cpu_map[i];
2177fa96bd91SMichael Corcoran }
2178fa96bd91SMichael Corcoran }
2179fa96bd91SMichael Corcoran if (rc == AE_OK) {
2180fa96bd91SMichael Corcoran if (item != NULL) {
2181fa96bd91SMichael Corcoran if (item->cpu_id == -1) {
2182fa96bd91SMichael Corcoran item->cpu_id = cpuid;
2183fa96bd91SMichael Corcoran } else {
2184fa96bd91SMichael Corcoran rc = AE_ALREADY_EXISTS;
2185fa96bd91SMichael Corcoran }
2186fa96bd91SMichael Corcoran } else if (cpu_map_count >= INT_MAX / 2) {
2187fa96bd91SMichael Corcoran rc = AE_NO_MEMORY;
2188fa96bd91SMichael Corcoran } else {
2189fa96bd91SMichael Corcoran acpica_grow_cpu_map();
2190fa96bd91SMichael Corcoran ASSERT(cpu_map != NULL);
2191fa96bd91SMichael Corcoran ASSERT(cpu_map_count < cpu_map_count_max);
2192fa96bd91SMichael Corcoran item = kmem_zalloc(sizeof (*item), KM_SLEEP);
2193fa96bd91SMichael Corcoran item->cpu_id = cpuid;
2194fa96bd91SMichael Corcoran item->proc_id = acpi_id;
2195fa96bd91SMichael Corcoran item->apic_id = UINT32_MAX;
2196fa96bd91SMichael Corcoran item->obj = NULL;
2197fa96bd91SMichael Corcoran cpu_map[cpu_map_count] = item;
2198fa96bd91SMichael Corcoran cpu_map_count++;
2199fa96bd91SMichael Corcoran }
2200fa96bd91SMichael Corcoran }
2201fa96bd91SMichael Corcoran mutex_exit(&cpu_map_lock);
2202fa96bd91SMichael Corcoran
2203fa96bd91SMichael Corcoran return (rc);
2204fa96bd91SMichael Corcoran }
2205fa96bd91SMichael Corcoran
2206fa96bd91SMichael Corcoran ACPI_STATUS
acpica_unmap_cpu(processorid_t cpuid)2207fa96bd91SMichael Corcoran acpica_unmap_cpu(processorid_t cpuid)
2208fa96bd91SMichael Corcoran {
2209fa96bd91SMichael Corcoran int i;
2210fa96bd91SMichael Corcoran ACPI_STATUS rc = AE_NOT_EXIST;
2211fa96bd91SMichael Corcoran
2212fa96bd91SMichael Corcoran ASSERT(cpuid != -1);
2213fa96bd91SMichael Corcoran if (cpuid == -1) {
2214fa96bd91SMichael Corcoran return (rc);
2215fa96bd91SMichael Corcoran }
2216fa96bd91SMichael Corcoran
2217fa96bd91SMichael Corcoran mutex_enter(&cpu_map_lock);
2218fa96bd91SMichael Corcoran for (i = 0; i < cpu_map_count; i++) {
2219fa96bd91SMichael Corcoran if (cpu_map[i]->cpu_id != cpuid) {
2220fa96bd91SMichael Corcoran continue;
2221fa96bd91SMichael Corcoran }
2222fa96bd91SMichael Corcoran cpu_map[i]->cpu_id = -1;
2223fa96bd91SMichael Corcoran /* Free item if no more reference. */
2224fa96bd91SMichael Corcoran if (cpu_map[i]->obj == NULL) {
2225fa96bd91SMichael Corcoran kmem_free(cpu_map[i], sizeof (struct cpu_map_item));
2226fa96bd91SMichael Corcoran cpu_map[i] = NULL;
2227fa96bd91SMichael Corcoran cpu_map_count--;
2228fa96bd91SMichael Corcoran if (i != cpu_map_count) {
2229fa96bd91SMichael Corcoran cpu_map[i] = cpu_map[cpu_map_count];
2230fa96bd91SMichael Corcoran cpu_map[cpu_map_count] = NULL;
2231fa96bd91SMichael Corcoran }
2232fa96bd91SMichael Corcoran }
2233fa96bd91SMichael Corcoran rc = AE_OK;
2234fa96bd91SMichael Corcoran break;
2235fa96bd91SMichael Corcoran }
2236fa96bd91SMichael Corcoran mutex_exit(&cpu_map_lock);
2237fa96bd91SMichael Corcoran
2238fa96bd91SMichael Corcoran return (rc);
2239fa96bd91SMichael Corcoran }
2240fa96bd91SMichael Corcoran
2241fa96bd91SMichael Corcoran ACPI_STATUS
acpica_get_cpu_object_by_cpuid(processorid_t cpuid,ACPI_HANDLE * hdlp)2242fa96bd91SMichael Corcoran acpica_get_cpu_object_by_cpuid(processorid_t cpuid, ACPI_HANDLE *hdlp)
2243fa96bd91SMichael Corcoran {
2244fa96bd91SMichael Corcoran int i;
2245fa96bd91SMichael Corcoran ACPI_STATUS rc = AE_NOT_EXIST;
2246fa96bd91SMichael Corcoran
2247fa96bd91SMichael Corcoran ASSERT(cpuid != -1);
2248fa96bd91SMichael Corcoran if (cpuid == -1) {
2249fa96bd91SMichael Corcoran return (rc);
2250fa96bd91SMichael Corcoran }
2251fa96bd91SMichael Corcoran
2252fa96bd91SMichael Corcoran mutex_enter(&cpu_map_lock);
2253fa96bd91SMichael Corcoran for (i = 0; i < cpu_map_count; i++) {
2254fa96bd91SMichael Corcoran if (cpu_map[i]->cpu_id == cpuid && cpu_map[i]->obj != NULL) {
2255fa96bd91SMichael Corcoran *hdlp = cpu_map[i]->obj;
2256fa96bd91SMichael Corcoran rc = AE_OK;
2257fa96bd91SMichael Corcoran break;
2258fa96bd91SMichael Corcoran }
2259fa96bd91SMichael Corcoran }
2260fa96bd91SMichael Corcoran mutex_exit(&cpu_map_lock);
2261fa96bd91SMichael Corcoran
2262fa96bd91SMichael Corcoran return (rc);
2263fa96bd91SMichael Corcoran }
2264fa96bd91SMichael Corcoran
2265fa96bd91SMichael Corcoran ACPI_STATUS
acpica_get_cpu_object_by_procid(UINT32 procid,ACPI_HANDLE * hdlp)2266fa96bd91SMichael Corcoran acpica_get_cpu_object_by_procid(UINT32 procid, ACPI_HANDLE *hdlp)
2267fa96bd91SMichael Corcoran {
2268fa96bd91SMichael Corcoran int i;
2269fa96bd91SMichael Corcoran ACPI_STATUS rc = AE_NOT_EXIST;
2270fa96bd91SMichael Corcoran
2271fa96bd91SMichael Corcoran mutex_enter(&cpu_map_lock);
2272fa96bd91SMichael Corcoran for (i = 0; i < cpu_map_count; i++) {
2273fa96bd91SMichael Corcoran if (cpu_map[i]->proc_id == procid && cpu_map[i]->obj != NULL) {
2274fa96bd91SMichael Corcoran *hdlp = cpu_map[i]->obj;
2275fa96bd91SMichael Corcoran rc = AE_OK;
2276fa96bd91SMichael Corcoran break;
2277fa96bd91SMichael Corcoran }
2278fa96bd91SMichael Corcoran }
2279fa96bd91SMichael Corcoran mutex_exit(&cpu_map_lock);
2280fa96bd91SMichael Corcoran
2281fa96bd91SMichael Corcoran return (rc);
2282fa96bd91SMichael Corcoran }
2283fa96bd91SMichael Corcoran
2284fa96bd91SMichael Corcoran ACPI_STATUS
acpica_get_cpu_object_by_apicid(UINT32 apicid,ACPI_HANDLE * hdlp)2285fa96bd91SMichael Corcoran acpica_get_cpu_object_by_apicid(UINT32 apicid, ACPI_HANDLE *hdlp)
2286fa96bd91SMichael Corcoran {
2287fa96bd91SMichael Corcoran int i;
2288fa96bd91SMichael Corcoran ACPI_STATUS rc = AE_NOT_EXIST;
2289fa96bd91SMichael Corcoran
2290fa96bd91SMichael Corcoran ASSERT(apicid != UINT32_MAX);
2291fa96bd91SMichael Corcoran if (apicid == UINT32_MAX) {
2292fa96bd91SMichael Corcoran return (rc);
2293fa96bd91SMichael Corcoran }
2294fa96bd91SMichael Corcoran
2295fa96bd91SMichael Corcoran mutex_enter(&cpu_map_lock);
2296fa96bd91SMichael Corcoran for (i = 0; i < cpu_map_count; i++) {
2297fa96bd91SMichael Corcoran if (cpu_map[i]->apic_id == apicid && cpu_map[i]->obj != NULL) {
2298fa96bd91SMichael Corcoran *hdlp = cpu_map[i]->obj;
2299fa96bd91SMichael Corcoran rc = AE_OK;
2300fa96bd91SMichael Corcoran break;
2301fa96bd91SMichael Corcoran }
2302fa96bd91SMichael Corcoran }
2303fa96bd91SMichael Corcoran mutex_exit(&cpu_map_lock);
2304fa96bd91SMichael Corcoran
2305fa96bd91SMichael Corcoran return (rc);
2306fa96bd91SMichael Corcoran }
2307fa96bd91SMichael Corcoran
2308a3114836SGerry Liu ACPI_STATUS
acpica_get_cpu_id_by_object(ACPI_HANDLE hdl,processorid_t * cpuidp)2309a3114836SGerry Liu acpica_get_cpu_id_by_object(ACPI_HANDLE hdl, processorid_t *cpuidp)
2310a3114836SGerry Liu {
2311a3114836SGerry Liu int i;
2312a3114836SGerry Liu ACPI_STATUS rc = AE_NOT_EXIST;
2313a3114836SGerry Liu
2314a3114836SGerry Liu ASSERT(cpuidp != NULL);
2315a3114836SGerry Liu if (hdl == NULL || cpuidp == NULL) {
2316a3114836SGerry Liu return (rc);
2317a3114836SGerry Liu }
2318a3114836SGerry Liu
2319a3114836SGerry Liu *cpuidp = -1;
2320a3114836SGerry Liu mutex_enter(&cpu_map_lock);
2321a3114836SGerry Liu for (i = 0; i < cpu_map_count; i++) {
2322a3114836SGerry Liu if (cpu_map[i]->obj == hdl && cpu_map[i]->cpu_id != -1) {
2323a3114836SGerry Liu *cpuidp = cpu_map[i]->cpu_id;
2324a3114836SGerry Liu rc = AE_OK;
2325a3114836SGerry Liu break;
2326a3114836SGerry Liu }
2327a3114836SGerry Liu }
2328a3114836SGerry Liu mutex_exit(&cpu_map_lock);
2329a3114836SGerry Liu
2330a3114836SGerry Liu return (rc);
2331a3114836SGerry Liu }
2332a3114836SGerry Liu
2333a3114836SGerry Liu ACPI_STATUS
acpica_get_apicid_by_object(ACPI_HANDLE hdl,UINT32 * rp)2334a3114836SGerry Liu acpica_get_apicid_by_object(ACPI_HANDLE hdl, UINT32 *rp)
2335a3114836SGerry Liu {
2336a3114836SGerry Liu int i;
2337a3114836SGerry Liu ACPI_STATUS rc = AE_NOT_EXIST;
2338a3114836SGerry Liu
2339a3114836SGerry Liu ASSERT(rp != NULL);
2340a3114836SGerry Liu if (hdl == NULL || rp == NULL) {
2341a3114836SGerry Liu return (rc);
2342a3114836SGerry Liu }
2343a3114836SGerry Liu
2344a3114836SGerry Liu *rp = UINT32_MAX;
2345a3114836SGerry Liu mutex_enter(&cpu_map_lock);
2346a3114836SGerry Liu for (i = 0; i < cpu_map_count; i++) {
2347a3114836SGerry Liu if (cpu_map[i]->obj == hdl &&
2348a3114836SGerry Liu cpu_map[i]->apic_id != UINT32_MAX) {
2349a3114836SGerry Liu *rp = cpu_map[i]->apic_id;
2350a3114836SGerry Liu rc = AE_OK;
2351a3114836SGerry Liu break;
2352a3114836SGerry Liu }
2353a3114836SGerry Liu }
2354a3114836SGerry Liu mutex_exit(&cpu_map_lock);
2355a3114836SGerry Liu
2356a3114836SGerry Liu return (rc);
2357a3114836SGerry Liu }
2358a3114836SGerry Liu
2359a3114836SGerry Liu ACPI_STATUS
acpica_get_procid_by_object(ACPI_HANDLE hdl,UINT32 * rp)2360a3114836SGerry Liu acpica_get_procid_by_object(ACPI_HANDLE hdl, UINT32 *rp)
2361a3114836SGerry Liu {
2362a3114836SGerry Liu int i;
2363a3114836SGerry Liu ACPI_STATUS rc = AE_NOT_EXIST;
2364a3114836SGerry Liu
2365a3114836SGerry Liu ASSERT(rp != NULL);
2366a3114836SGerry Liu if (hdl == NULL || rp == NULL) {
2367a3114836SGerry Liu return (rc);
2368a3114836SGerry Liu }
2369a3114836SGerry Liu
2370a3114836SGerry Liu *rp = UINT32_MAX;
2371a3114836SGerry Liu mutex_enter(&cpu_map_lock);
2372a3114836SGerry Liu for (i = 0; i < cpu_map_count; i++) {
2373a3114836SGerry Liu if (cpu_map[i]->obj == hdl) {
2374a3114836SGerry Liu *rp = cpu_map[i]->proc_id;
2375a3114836SGerry Liu rc = AE_OK;
2376a3114836SGerry Liu break;
2377a3114836SGerry Liu }
2378a3114836SGerry Liu }
2379a3114836SGerry Liu mutex_exit(&cpu_map_lock);
2380a3114836SGerry Liu
2381a3114836SGerry Liu return (rc);
2382a3114836SGerry Liu }
2383a3114836SGerry Liu
2384fa96bd91SMichael Corcoran void
acpica_set_core_feature(uint64_t features)2385fa96bd91SMichael Corcoran acpica_set_core_feature(uint64_t features)
2386fa96bd91SMichael Corcoran {
2387fa96bd91SMichael Corcoran atomic_or_64(&acpica_core_features, features);
2388fa96bd91SMichael Corcoran }
2389fa96bd91SMichael Corcoran
2390fa96bd91SMichael Corcoran void
acpica_clear_core_feature(uint64_t features)2391fa96bd91SMichael Corcoran acpica_clear_core_feature(uint64_t features)
2392fa96bd91SMichael Corcoran {
2393fa96bd91SMichael Corcoran atomic_and_64(&acpica_core_features, ~features);
2394fa96bd91SMichael Corcoran }
2395fa96bd91SMichael Corcoran
2396fa96bd91SMichael Corcoran uint64_t
acpica_get_core_feature(uint64_t features)2397fa96bd91SMichael Corcoran acpica_get_core_feature(uint64_t features)
2398fa96bd91SMichael Corcoran {
2399fa96bd91SMichael Corcoran return (acpica_core_features & features);
2400fa96bd91SMichael Corcoran }
2401fa96bd91SMichael Corcoran
2402fa96bd91SMichael Corcoran void
acpica_set_devcfg_feature(uint64_t features)2403fa96bd91SMichael Corcoran acpica_set_devcfg_feature(uint64_t features)
2404fa96bd91SMichael Corcoran {
2405fa96bd91SMichael Corcoran atomic_or_64(&acpica_devcfg_features, features);
2406fa96bd91SMichael Corcoran }
2407fa96bd91SMichael Corcoran
2408fa96bd91SMichael Corcoran void
acpica_clear_devcfg_feature(uint64_t features)2409fa96bd91SMichael Corcoran acpica_clear_devcfg_feature(uint64_t features)
2410fa96bd91SMichael Corcoran {
2411fa96bd91SMichael Corcoran atomic_and_64(&acpica_devcfg_features, ~features);
2412fa96bd91SMichael Corcoran }
2413fa96bd91SMichael Corcoran
2414fa96bd91SMichael Corcoran uint64_t
acpica_get_devcfg_feature(uint64_t features)2415fa96bd91SMichael Corcoran acpica_get_devcfg_feature(uint64_t features)
2416fa96bd91SMichael Corcoran {
2417fa96bd91SMichael Corcoran return (acpica_devcfg_features & features);
24185cff7825Smh }
24190e751525SEric Saxe
24200e751525SEric Saxe void
acpica_get_global_FADT(ACPI_TABLE_FADT ** gbl_FADT)24210e751525SEric Saxe acpica_get_global_FADT(ACPI_TABLE_FADT **gbl_FADT)
24220e751525SEric Saxe {
24230e751525SEric Saxe *gbl_FADT = &AcpiGbl_FADT;
24240e751525SEric Saxe }
242578d5422cSMark Haywood
242678d5422cSMark Haywood void
acpica_write_cpupm_capabilities(boolean_t pstates,boolean_t cstates)242778d5422cSMark Haywood acpica_write_cpupm_capabilities(boolean_t pstates, boolean_t cstates)
242878d5422cSMark Haywood {
242978d5422cSMark Haywood if (pstates && AcpiGbl_FADT.PstateControl != 0)
243078d5422cSMark Haywood (void) AcpiHwRegisterWrite(ACPI_REGISTER_SMI_COMMAND_BLOCK,
243178d5422cSMark Haywood AcpiGbl_FADT.PstateControl);
243278d5422cSMark Haywood
243378d5422cSMark Haywood if (cstates && AcpiGbl_FADT.CstControl != 0)
243478d5422cSMark Haywood (void) AcpiHwRegisterWrite(ACPI_REGISTER_SMI_COMMAND_BLOCK,
243578d5422cSMark Haywood AcpiGbl_FADT.CstControl);
243678d5422cSMark Haywood }
24377b1019a6SJerry Jelinek
24387b1019a6SJerry Jelinek uint32_t
acpi_strtoul(const char * str,char ** ep,int base)24397b1019a6SJerry Jelinek acpi_strtoul(const char *str, char **ep, int base)
24407b1019a6SJerry Jelinek {
24417b1019a6SJerry Jelinek ulong_t v;
24427b1019a6SJerry Jelinek
24437b1019a6SJerry Jelinek if (ddi_strtoul(str, ep, base, &v) != 0 || v > ACPI_UINT32_MAX) {
24447b1019a6SJerry Jelinek return (ACPI_UINT32_MAX);
24457b1019a6SJerry Jelinek }
24467b1019a6SJerry Jelinek
24477b1019a6SJerry Jelinek return ((uint32_t)v);
24487b1019a6SJerry Jelinek }
244935786f68SRobert Mustacchi
245035786f68SRobert Mustacchi /*
245135786f68SRobert Mustacchi * In prior versions of ACPI, the AcpiGetObjectInfo() function would provide
245235786f68SRobert Mustacchi * information about the status of the object via the _STA method. This has been
245335786f68SRobert Mustacchi * removed and this function is used to replace.
245435786f68SRobert Mustacchi *
245535786f68SRobert Mustacchi * Not every ACPI object has a _STA method. In cases where it is not found, then
245635786f68SRobert Mustacchi * the OSPM (aka us) is supposed to interpret that as though it indicates that
245735786f68SRobert Mustacchi * the device is present, enabled, shown in the UI, and functioning. This is the
245835786f68SRobert Mustacchi * value 0xF.
245935786f68SRobert Mustacchi */
246035786f68SRobert Mustacchi ACPI_STATUS
acpica_get_object_status(ACPI_HANDLE obj,int * statusp)246135786f68SRobert Mustacchi acpica_get_object_status(ACPI_HANDLE obj, int *statusp)
246235786f68SRobert Mustacchi {
246335786f68SRobert Mustacchi ACPI_STATUS status;
246435786f68SRobert Mustacchi int ival;
246535786f68SRobert Mustacchi
246635786f68SRobert Mustacchi status = acpica_eval_int(obj, METHOD_NAME__STA, &ival);
246735786f68SRobert Mustacchi if (ACPI_FAILURE(status)) {
246835786f68SRobert Mustacchi if (status == AE_NOT_FOUND) {
246935786f68SRobert Mustacchi *statusp = 0xf;
247035786f68SRobert Mustacchi return (AE_OK);
247135786f68SRobert Mustacchi }
247235786f68SRobert Mustacchi
247335786f68SRobert Mustacchi return (status);
247435786f68SRobert Mustacchi }
247535786f68SRobert Mustacchi
247635786f68SRobert Mustacchi /*
247735786f68SRobert Mustacchi * This should not be a negative value. However, firmware is often the
247835786f68SRobert Mustacchi * enemy. If it does, complain and treat that as a hard failure.
247935786f68SRobert Mustacchi */
248035786f68SRobert Mustacchi if (ival < 0) {
248135786f68SRobert Mustacchi cmn_err(CE_WARN, "!acpica_get_object_status: encountered "
248235786f68SRobert Mustacchi "negative _STA value on obj %p", obj);
248335786f68SRobert Mustacchi return (AE_ERROR);
248435786f68SRobert Mustacchi }
248535786f68SRobert Mustacchi
248635786f68SRobert Mustacchi *statusp = ival;
248735786f68SRobert Mustacchi return (AE_OK);
248835786f68SRobert Mustacchi }
2489