Home | History | Annotate | Line # | Download | only in acpi
acpi_util.c revision 1.29
      1 /*	$NetBSD: acpi_util.c,v 1.29 2021/12/31 13:15:00 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2003, 2007, 2021 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum of By Noon Software, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright 2001, 2003 Wasabi Systems, Inc.
     34  * All rights reserved.
     35  *
     36  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. All advertising materials mentioning features or use of this software
     47  *    must display the following acknowledgement:
     48  *	This product includes software developed for the NetBSD Project by
     49  *	Wasabi Systems, Inc.
     50  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     51  *    or promote products derived from this software without specific prior
     52  *    written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     57  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     58  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     59  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     60  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     61  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     62  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     63  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     64  * POSSIBILITY OF SUCH DAMAGE.
     65  */
     66 
     67 #include <sys/cdefs.h>
     68 __KERNEL_RCSID(0, "$NetBSD: acpi_util.c,v 1.29 2021/12/31 13:15:00 jmcneill Exp $");
     69 
     70 #include <sys/param.h>
     71 #include <sys/kmem.h>
     72 #include <sys/cpu.h>
     73 
     74 #include <dev/acpi/acpireg.h>
     75 #include <dev/acpi/acpivar.h>
     76 #include <dev/acpi/acpi_intr.h>
     77 
     78 #include <sys/device_calls.h>
     79 
     80 #include <machine/acpi_machdep.h>
     81 
     82 #define _COMPONENT	ACPI_BUS_COMPONENT
     83 ACPI_MODULE_NAME	("acpi_util")
     84 
     85 static void		acpi_clean_node(ACPI_HANDLE, void *);
     86 static ACPI_STATUS	acpi_dsd_property(ACPI_HANDLE, const char *,
     87 			    ACPI_BUFFER *, ACPI_OBJECT_TYPE, ACPI_OBJECT **);
     88 
     89 static const char * const acpicpu_ids[] = {
     90 	"ACPI0007",
     91 	NULL
     92 };
     93 
     94 static const struct device_compatible_entry dtlink_compat_data[] = {
     95 	{ .compat = "PRP0001" },
     96 	DEVICE_COMPAT_EOL
     97 };
     98 
     99 /*
    100  * ACPI device handle support.
    101  */
    102 
    103 static device_call_t
    104 acpi_devhandle_lookup_device_call(devhandle_t handle, const char *name,
    105     devhandle_t *call_handlep)
    106 {
    107 	__link_set_decl(acpi_device_calls, struct device_call_descriptor);
    108 	struct device_call_descriptor * const *desc;
    109 
    110 	__link_set_foreach(desc, acpi_device_calls) {
    111 		if (strcmp((*desc)->name, name) == 0) {
    112 			return (*desc)->call;
    113 		}
    114 	}
    115 	return NULL;
    116 }
    117 
    118 static const struct devhandle_impl acpi_devhandle_impl = {
    119 	.type = DEVHANDLE_TYPE_ACPI,
    120 	.lookup_device_call = acpi_devhandle_lookup_device_call,
    121 };
    122 
    123 devhandle_t
    124 devhandle_from_acpi(ACPI_HANDLE const hdl)
    125 {
    126 	devhandle_t handle = {
    127 		.impl = &acpi_devhandle_impl,
    128 		.pointer = hdl,
    129 	};
    130 
    131 	return handle;
    132 }
    133 
    134 ACPI_HANDLE
    135 devhandle_to_acpi(devhandle_t const handle)
    136 {
    137 	KASSERT(devhandle_type(handle) == DEVHANDLE_TYPE_ACPI);
    138 
    139 	return handle.pointer;
    140 }
    141 
    142 static int
    143 acpi_device_enumerate_children(device_t dev, devhandle_t call_handle, void *v)
    144 {
    145 	struct device_enumerate_children_args *args = v;
    146 	ACPI_HANDLE hdl = devhandle_to_acpi(call_handle);
    147 	struct acpi_devnode *devnode, *ad;
    148 
    149 	devnode = acpi_match_node(hdl);
    150 	KASSERT(devnode != NULL);
    151 
    152 	SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
    153 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE ||
    154 		    !acpi_device_present(ad->ad_handle)) {
    155 			continue;
    156 		}
    157 		if (!args->callback(dev, devhandle_from_acpi(ad->ad_handle),
    158 				    args->callback_arg)) {
    159 			break;
    160 		}
    161 	}
    162 
    163 	return 0;
    164 }
    165 ACPI_DEVICE_CALL_REGISTER(DEVICE_ENUMERATE_CHILDREN_STR,
    166 			  acpi_device_enumerate_children)
    167 
    168 /*
    169  * Evaluate an integer object.
    170  */
    171 ACPI_STATUS
    172 acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
    173 {
    174 	ACPI_OBJECT obj;
    175 	ACPI_BUFFER buf;
    176 	ACPI_STATUS rv;
    177 
    178 	if (handle == NULL)
    179 		handle = ACPI_ROOT_OBJECT;
    180 
    181 	(void)memset(&obj, 0, sizeof(obj));
    182 	buf.Pointer = &obj;
    183 	buf.Length = sizeof(obj);
    184 
    185 	rv = AcpiEvaluateObject(handle, path, NULL, &buf);
    186 
    187 	if (ACPI_FAILURE(rv))
    188 		return rv;
    189 
    190 	/* Check that evaluation produced a return value. */
    191 	if (buf.Length == 0)
    192 		return AE_NULL_OBJECT;
    193 
    194 	if (obj.Type != ACPI_TYPE_INTEGER)
    195 		return AE_TYPE;
    196 
    197 	if (valp != NULL)
    198 		*valp = obj.Integer.Value;
    199 
    200 	return AE_OK;
    201 }
    202 
    203 /*
    204  * Evaluate an integer object with a single integer input parameter.
    205  */
    206 ACPI_STATUS
    207 acpi_eval_set_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER val)
    208 {
    209 	ACPI_OBJECT_LIST arg;
    210 	ACPI_OBJECT obj;
    211 
    212 	if (handle == NULL)
    213 		handle = ACPI_ROOT_OBJECT;
    214 
    215 	obj.Type = ACPI_TYPE_INTEGER;
    216 	obj.Integer.Value = val;
    217 
    218 	arg.Count = 1;
    219 	arg.Pointer = &obj;
    220 
    221 	return AcpiEvaluateObject(handle, path, &arg, NULL);
    222 }
    223 
    224 /*
    225  * Evaluate a (Unicode) string object.
    226  */
    227 ACPI_STATUS
    228 acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
    229 {
    230 	ACPI_OBJECT *obj;
    231 	ACPI_BUFFER buf;
    232 	ACPI_STATUS rv;
    233 
    234 	rv = acpi_eval_struct(handle, path, &buf);
    235 
    236 	if (ACPI_FAILURE(rv))
    237 		return rv;
    238 
    239 	obj = buf.Pointer;
    240 
    241 	if (obj->Type != ACPI_TYPE_STRING) {
    242 		rv = AE_TYPE;
    243 		goto out;
    244 	}
    245 
    246 	if (obj->String.Length == 0) {
    247 		rv = AE_BAD_DATA;
    248 		goto out;
    249 	}
    250 
    251 	*stringp = ACPI_ALLOCATE(obj->String.Length + 1);
    252 
    253 	if (*stringp == NULL) {
    254 		rv = AE_NO_MEMORY;
    255 		goto out;
    256 	}
    257 
    258 	(void)memcpy(*stringp, obj->String.Pointer, obj->String.Length);
    259 
    260 	(*stringp)[obj->String.Length] = '\0';
    261 
    262 out:
    263 	ACPI_FREE(buf.Pointer);
    264 
    265 	return rv;
    266 }
    267 
    268 /*
    269  * Evaluate a structure. Caller must free buf.Pointer by ACPI_FREE().
    270  */
    271 ACPI_STATUS
    272 acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *buf)
    273 {
    274 
    275 	if (handle == NULL)
    276 		handle = ACPI_ROOT_OBJECT;
    277 
    278 	buf->Pointer = NULL;
    279 	buf->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    280 
    281 	return AcpiEvaluateObject(handle, path, NULL, buf);
    282 }
    283 
    284 /*
    285  * Evaluate a reference handle from an element in a package.
    286  */
    287 ACPI_STATUS
    288 acpi_eval_reference_handle(ACPI_OBJECT *elm, ACPI_HANDLE *handle)
    289 {
    290 
    291 	if (elm == NULL || handle == NULL)
    292 		return AE_BAD_PARAMETER;
    293 
    294 	switch (elm->Type) {
    295 
    296 	case ACPI_TYPE_ANY:
    297 	case ACPI_TYPE_LOCAL_REFERENCE:
    298 
    299 		if (elm->Reference.Handle == NULL)
    300 			return AE_NULL_ENTRY;
    301 
    302 		*handle = elm->Reference.Handle;
    303 
    304 		return AE_OK;
    305 
    306 	case ACPI_TYPE_STRING:
    307 		return AcpiGetHandle(NULL, elm->String.Pointer, handle);
    308 
    309 	default:
    310 		return AE_TYPE;
    311 	}
    312 }
    313 
    314 /*
    315  * Iterate over all objects in a package, and pass them all
    316  * to a function. If the called function returns non-AE_OK,
    317  * the iteration is stopped and that value is returned.
    318  */
    319 ACPI_STATUS
    320 acpi_foreach_package_object(ACPI_OBJECT *pkg,
    321     ACPI_STATUS (*func)(ACPI_OBJECT *, void *), void *arg)
    322 {
    323 	ACPI_STATUS rv = AE_OK;
    324 	uint32_t i;
    325 
    326 	if (pkg == NULL)
    327 		return AE_BAD_PARAMETER;
    328 
    329 	if (pkg->Type != ACPI_TYPE_PACKAGE)
    330 		return AE_TYPE;
    331 
    332 	for (i = 0; i < pkg->Package.Count; i++) {
    333 
    334 		rv = (*func)(&pkg->Package.Elements[i], arg);
    335 
    336 		if (ACPI_FAILURE(rv))
    337 			break;
    338 	}
    339 
    340 	return rv;
    341 }
    342 
    343 /*
    344  * Fetch data info the specified (empty) ACPI buffer.
    345  * Caller must free buf.Pointer by ACPI_FREE().
    346  */
    347 ACPI_STATUS
    348 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
    349     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
    350 {
    351 
    352 	buf->Pointer = NULL;
    353 	buf->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    354 
    355 	return (*getit)(handle, buf);
    356 }
    357 
    358 /*
    359  * Return a complete pathname from a handle.
    360  *
    361  * Note that the function uses static data storage;
    362  * if the data is needed for future use, it should be
    363  * copied before any subsequent calls overwrite it.
    364  */
    365 const char *
    366 acpi_name(ACPI_HANDLE handle)
    367 {
    368 	static char name[80];
    369 	ACPI_BUFFER buf;
    370 	ACPI_STATUS rv;
    371 
    372 	if (handle == NULL)
    373 		handle = ACPI_ROOT_OBJECT;
    374 
    375 	buf.Pointer = name;
    376 	buf.Length = sizeof(name);
    377 
    378 	rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
    379 
    380 	if (ACPI_FAILURE(rv))
    381 		return "UNKNOWN";
    382 
    383 	return name;
    384 }
    385 
    386 /*
    387  * Pack _HID and _CID ID strings into an OpenFirmware-style
    388  * string list.
    389  */
    390 char *
    391 acpi_pack_compat_list(ACPI_DEVICE_INFO *ad, size_t *sizep)
    392 {
    393 	KASSERT(sizep != NULL);
    394 
    395 	char *sl = NULL;
    396 	size_t slsize = 0;
    397 	uint32_t i;
    398 
    399 	if ((ad->Valid & ACPI_VALID_HID) != 0) {
    400 		strlist_append(&sl, &slsize, ad->HardwareId.String);
    401 	}
    402 
    403 	if ((ad->Valid & ACPI_VALID_CID) != 0) {
    404 		for (i = 0; i < ad->CompatibleIdList.Count; i++) {
    405 			strlist_append(&sl, &slsize,
    406 			    ad->CompatibleIdList.Ids[i].String);
    407 		}
    408 	}
    409 
    410 	*sizep = slsize;
    411 	return sl;
    412 }
    413 
    414 /*
    415  * The ACPI_PNP_DEVICE_ID type is somewhat inconvenient for us to
    416  * use.  We'll need some temporary space to pack it into an array
    417  * of C strings.  Room for 8 should be plenty, but we can allocate
    418  * more if necessary.
    419  */
    420 #define	ACPI_COMPATSTR_MAX	8
    421 
    422 static const char **
    423 acpi_compatible_alloc_strarray(ACPI_PNP_DEVICE_ID *ids,
    424     unsigned int count, const char **buf)
    425 {
    426 	unsigned int i;
    427 
    428 	buf = kmem_tmpbuf_alloc(count * sizeof(const char *),
    429 	    buf, ACPI_COMPATSTR_MAX * sizeof(const char *), KM_SLEEP);
    430 	for (i = 0; i < count; i++) {
    431 		buf[i] = ids[i].String;
    432 	}
    433 	return buf;
    434 }
    435 
    436 static void
    437 acpi_compatible_free_strarray(const char **cpp, unsigned int count,
    438     const char **buf)
    439 {
    440 	kmem_tmpbuf_free(cpp, count * sizeof(const char *), buf);
    441 }
    442 
    443 static int
    444 acpi_compatible_match_dtlink(const struct acpi_attach_args * const aa,
    445     const struct device_compatible_entry * const dce)
    446 {
    447 	const char *strings[ACPI_COMPATSTR_MAX * sizeof(const char *)];
    448 	ACPI_HANDLE handle = aa->aa_node->ad_handle;
    449 	ACPI_BUFFER buf;
    450 	char *compatible;
    451 	ACPI_STATUS ret;
    452 	ACPI_OBJECT *obj;
    453 	int rv = 0, n;
    454 
    455 	buf.Pointer = NULL;
    456 	buf.Length = ACPI_ALLOCATE_BUFFER;
    457 
    458 	/* Match a single string _DSD value */
    459 	ret = acpi_dsd_string(handle, "compatible", &compatible);
    460 	if (ACPI_SUCCESS(ret)) {
    461 		strings[0] = compatible;
    462 		rv = device_compatible_pmatch(strings, 1, dce);
    463 		kmem_strfree(compatible);
    464 		goto done;
    465 	}
    466 
    467 	/* Match from a list of strings in a _DSD value */
    468 	ret = acpi_dsd_property(handle, "compatible", &buf,
    469 	    ACPI_TYPE_PACKAGE, &obj);
    470 	if (ACPI_FAILURE(ret)) {
    471 		goto done;
    472 	}
    473 	if (obj->Package.Count == 0) {
    474 		goto done;
    475 	}
    476 	for (n = 0; n < imin(obj->Package.Count, ACPI_COMPATSTR_MAX); n++) {
    477 		if (obj->Package.Elements[n].Type != ACPI_TYPE_STRING) {
    478 			goto done;
    479 		}
    480 		strings[n] = obj->Package.Elements[n].String.Pointer;
    481 	}
    482 	rv = device_compatible_pmatch(strings, n, dce);
    483 
    484 done:
    485 	if (buf.Pointer != NULL) {
    486 		ACPI_FREE(buf.Pointer);
    487 	}
    488 	if (rv) {
    489 		rv = (rv - 1) + ACPI_MATCHSCORE_CID;
    490 		return imin(rv, ACPI_MATCHSCORE_CID_MAX);
    491 	}
    492 	return 0;
    493 }
    494 
    495 /*
    496  * acpi_compatible_match --
    497  *
    498  *	Returns a weighted match value, comparing the _HID and _CID
    499  *	IDs against a driver's compatibility data.
    500  */
    501 int
    502 acpi_compatible_match(const struct acpi_attach_args * const aa,
    503     const struct device_compatible_entry * const dce)
    504 {
    505 	const char *strings[ACPI_COMPATSTR_MAX * sizeof(const char *)];
    506 	const char **cpp;
    507 	bool dtlink = false;
    508 	int rv;
    509 
    510 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) {
    511 		return 0;
    512 	}
    513 
    514 	ACPI_DEVICE_INFO *ad = aa->aa_node->ad_devinfo;
    515 
    516 	if ((ad->Valid & ACPI_VALID_HID) != 0) {
    517 		strings[0] = ad->HardwareId.String;
    518 
    519 		/* Matching _HID wins big. */
    520 		if (device_compatible_pmatch(strings, 1, dce) != 0) {
    521 			return ACPI_MATCHSCORE_HID;
    522 		}
    523 
    524 		if (device_compatible_pmatch(strings, 1,
    525 					     dtlink_compat_data) != 0) {
    526 			dtlink = true;
    527 		}
    528 	}
    529 
    530 	if ((ad->Valid & ACPI_VALID_CID) != 0) {
    531 		cpp = acpi_compatible_alloc_strarray(ad->CompatibleIdList.Ids,
    532 		    ad->CompatibleIdList.Count, strings);
    533 
    534 		rv = device_compatible_pmatch(cpp,
    535 		    ad->CompatibleIdList.Count, dce);
    536 		if (!dtlink &&
    537 		    device_compatible_pmatch(cpp, ad->CompatibleIdList.Count,
    538 					     dtlink_compat_data) != 0) {
    539 			dtlink = true;
    540 		}
    541 		acpi_compatible_free_strarray(cpp, ad->CompatibleIdList.Count,
    542 		    strings);
    543 		if (rv) {
    544 			rv = (rv - 1) + ACPI_MATCHSCORE_CID;
    545 			return imin(rv, ACPI_MATCHSCORE_CID_MAX);
    546 		}
    547 	}
    548 
    549 	if (dtlink) {
    550 		return acpi_compatible_match_dtlink(aa, dce);
    551 	}
    552 
    553 	return 0;
    554 }
    555 
    556 /*
    557  * acpi_compatible_lookup --
    558  *
    559  *	Returns the device_compatible_entry that matches the _HID
    560  *	or _CID ID.
    561  */
    562 const struct device_compatible_entry *
    563 acpi_compatible_lookup(const struct acpi_attach_args * const aa,
    564     const struct device_compatible_entry * const dce)
    565 {
    566 	const struct device_compatible_entry *rv = NULL;
    567 	const char *strings[ACPI_COMPATSTR_MAX];
    568 	const char **cpp;
    569 
    570 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) {
    571 		return NULL;
    572 	}
    573 
    574 	ACPI_DEVICE_INFO *ad = aa->aa_node->ad_devinfo;
    575 
    576 	if ((ad->Valid & ACPI_VALID_HID) != 0) {
    577 		strings[0] = ad->HardwareId.String;
    578 
    579 		rv = device_compatible_plookup(strings, 1, dce);
    580 		if (rv != NULL)
    581 			return rv;
    582 	}
    583 
    584 	if ((ad->Valid & ACPI_VALID_CID) != 0) {
    585 		cpp = acpi_compatible_alloc_strarray(ad->CompatibleIdList.Ids,
    586 		    ad->CompatibleIdList.Count, strings);
    587 
    588 		rv = device_compatible_plookup(cpp,
    589 		    ad->CompatibleIdList.Count, dce);
    590 		acpi_compatible_free_strarray(cpp, ad->CompatibleIdList.Count,
    591 		    strings);
    592 	}
    593 
    594 	return rv;
    595 }
    596 
    597 /*
    598  * Match given IDs against _HID and _CIDs.
    599  */
    600 int
    601 acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
    602 {
    603 	uint32_t i, n;
    604 	char *id;
    605 
    606 	while (*ids) {
    607 
    608 		if ((ad->Valid & ACPI_VALID_HID) != 0) {
    609 
    610 			if (pmatch(ad->HardwareId.String, *ids, NULL) == 2)
    611 				return 1;
    612 		}
    613 
    614 		if ((ad->Valid & ACPI_VALID_CID) != 0) {
    615 
    616 			n = ad->CompatibleIdList.Count;
    617 
    618 			for (i = 0; i < n; i++) {
    619 
    620 				id = ad->CompatibleIdList.Ids[i].String;
    621 
    622 				if (pmatch(id, *ids, NULL) == 2)
    623 					return 1;
    624 			}
    625 		}
    626 
    627 		ids++;
    628 	}
    629 
    630 	return 0;
    631 }
    632 
    633 /*
    634  * Match a PCI-defined bass-class, sub-class, and programming interface
    635  * against a handle's _CLS object.
    636  */
    637 int
    638 acpi_match_class(ACPI_HANDLE handle, uint8_t pci_class, uint8_t pci_subclass,
    639     uint8_t pci_interface)
    640 {
    641 	ACPI_BUFFER buf;
    642 	ACPI_OBJECT *obj;
    643 	ACPI_STATUS rv;
    644 	int match = 0;
    645 
    646 	rv = acpi_eval_struct(handle, "_CLS", &buf);
    647 	if (ACPI_FAILURE(rv))
    648 		goto done;
    649 
    650 	obj = buf.Pointer;
    651 	if (obj->Type != ACPI_TYPE_PACKAGE)
    652 		goto done;
    653 	if (obj->Package.Count != 3)
    654 		goto done;
    655 	if (obj->Package.Elements[0].Type != ACPI_TYPE_INTEGER ||
    656 	    obj->Package.Elements[1].Type != ACPI_TYPE_INTEGER ||
    657 	    obj->Package.Elements[2].Type != ACPI_TYPE_INTEGER)
    658 		goto done;
    659 
    660 	match = obj->Package.Elements[0].Integer.Value == pci_class &&
    661 		obj->Package.Elements[1].Integer.Value == pci_subclass &&
    662 		obj->Package.Elements[2].Integer.Value == pci_interface;
    663 
    664 done:
    665 	if (buf.Pointer)
    666 		ACPI_FREE(buf.Pointer);
    667 	return match ? ACPI_MATCHSCORE_CLS : 0;
    668 }
    669 
    670 /*
    671  * Match a device node from a handle.
    672  */
    673 struct acpi_devnode *
    674 acpi_match_node(ACPI_HANDLE handle)
    675 {
    676 	struct acpi_devnode *ad;
    677 	ACPI_STATUS rv;
    678 
    679 	if (handle == NULL)
    680 		return NULL;
    681 
    682 	rv = AcpiGetData(handle, acpi_clean_node, (void **)&ad);
    683 
    684 	if (ACPI_FAILURE(rv))
    685 		return NULL;
    686 
    687 	return ad;
    688 }
    689 
    690 /*
    691  * Permanently associate a device node with a handle.
    692  */
    693 void
    694 acpi_match_node_init(struct acpi_devnode *ad)
    695 {
    696 	(void)AcpiAttachData(ad->ad_handle, acpi_clean_node, ad);
    697 }
    698 
    699 static void
    700 acpi_clean_node(ACPI_HANDLE handle, void *aux)
    701 {
    702 	/* Nothing. */
    703 }
    704 
    705 /*
    706  * Match a handle from a cpu_info. Returns NULL on failure.
    707  *
    708  * Note that acpi_match_node() can be used if the device node
    709  * is also required.
    710  */
    711 ACPI_HANDLE
    712 acpi_match_cpu_info(struct cpu_info *ci)
    713 {
    714 	struct acpi_softc *sc = acpi_softc;
    715 	struct acpi_devnode *ad;
    716 	ACPI_INTEGER val;
    717 	ACPI_OBJECT *obj;
    718 	ACPI_BUFFER buf;
    719 	ACPI_HANDLE hdl;
    720 	ACPI_STATUS rv;
    721 
    722 	if (sc == NULL || acpi_active == 0)
    723 		return NULL;
    724 
    725 	/*
    726 	 * CPUs are declared in the ACPI namespace
    727 	 * either as a Processor() or as a Device().
    728 	 * In both cases the MADT entries are used
    729 	 * for the match (see ACPI 4.0, section 8.4).
    730 	 */
    731 	SIMPLEQ_FOREACH(ad, &sc->sc_head, ad_list) {
    732 
    733 		hdl = ad->ad_handle;
    734 
    735 		switch (ad->ad_type) {
    736 
    737 		case ACPI_TYPE_DEVICE:
    738 
    739 			if (acpi_match_hid(ad->ad_devinfo, acpicpu_ids) == 0)
    740 				break;
    741 
    742 			rv = acpi_eval_integer(hdl, "_UID", &val);
    743 
    744 			if (ACPI_SUCCESS(rv) && val == ci->ci_acpiid)
    745 				return hdl;
    746 
    747 			break;
    748 
    749 		case ACPI_TYPE_PROCESSOR:
    750 
    751 			rv = acpi_eval_struct(hdl, NULL, &buf);
    752 
    753 			if (ACPI_FAILURE(rv))
    754 				break;
    755 
    756 			obj = buf.Pointer;
    757 
    758 			if (obj->Processor.ProcId == ci->ci_acpiid) {
    759 				ACPI_FREE(buf.Pointer);
    760 				return hdl;
    761 			}
    762 
    763 			ACPI_FREE(buf.Pointer);
    764 			break;
    765 		}
    766 	}
    767 
    768 	return NULL;
    769 }
    770 
    771 /*
    772  * Match a CPU from a handle. Returns NULL on failure.
    773  */
    774 struct cpu_info *
    775 acpi_match_cpu_handle(ACPI_HANDLE hdl)
    776 {
    777 	struct cpu_info *ci;
    778 	ACPI_DEVICE_INFO *di;
    779 	CPU_INFO_ITERATOR cii;
    780 	ACPI_INTEGER val;
    781 	ACPI_OBJECT *obj;
    782 	ACPI_BUFFER buf;
    783 	ACPI_STATUS rv;
    784 
    785 	ci = NULL;
    786 	di = NULL;
    787 	buf.Pointer = NULL;
    788 
    789 	rv = AcpiGetObjectInfo(hdl, &di);
    790 
    791 	if (ACPI_FAILURE(rv))
    792 		return NULL;
    793 
    794 	switch (di->Type) {
    795 
    796 	case ACPI_TYPE_DEVICE:
    797 
    798 		if (acpi_match_hid(di, acpicpu_ids) == 0)
    799 			goto out;
    800 
    801 		rv = acpi_eval_integer(hdl, "_UID", &val);
    802 
    803 		if (ACPI_FAILURE(rv))
    804 			goto out;
    805 
    806 		break;
    807 
    808 	case ACPI_TYPE_PROCESSOR:
    809 
    810 		rv = acpi_eval_struct(hdl, NULL, &buf);
    811 
    812 		if (ACPI_FAILURE(rv))
    813 			goto out;
    814 
    815 		obj = buf.Pointer;
    816 		val = obj->Processor.ProcId;
    817 		break;
    818 
    819 	default:
    820 		goto out;
    821 	}
    822 
    823 	for (CPU_INFO_FOREACH(cii, ci)) {
    824 
    825 		if (ci->ci_acpiid == val)
    826 			goto out;
    827 	}
    828 
    829 	ci = NULL;
    830 
    831 out:
    832 	if (di != NULL)
    833 		ACPI_FREE(di);
    834 
    835 	if (buf.Pointer != NULL)
    836 		ACPI_FREE(buf.Pointer);
    837 
    838 	return ci;
    839 }
    840 
    841 struct acpi_irq_handler {
    842 	uint32_t aih_irq;
    843 	void *aih_ih;
    844 };
    845 
    846 void *
    847 acpi_intr_establish(device_t dev, uint64_t c, int ipl, bool mpsafe,
    848     int (*intr)(void *), void *iarg, const char *xname)
    849 {
    850 	ACPI_STATUS rv;
    851 	ACPI_HANDLE hdl = (void *)(uintptr_t)c;
    852 	struct acpi_resources res;
    853 	struct acpi_irq *irq;
    854 	void *aih = NULL;
    855 
    856 	rv = acpi_resource_parse(dev, hdl, "_CRS", &res,
    857 	    &acpi_resource_parse_ops_quiet);
    858 	if (ACPI_FAILURE(rv))
    859 		return NULL;
    860 
    861 	irq = acpi_res_irq(&res, 0);
    862 	if (irq == NULL)
    863 		goto end;
    864 
    865 	aih = acpi_intr_establish_irq(dev, irq, ipl, mpsafe,
    866 	    intr, iarg, xname);
    867 
    868 end:
    869 	acpi_resource_cleanup(&res);
    870 
    871 	return aih;
    872 }
    873 
    874 void *
    875 acpi_intr_establish_irq(device_t dev, struct acpi_irq *irq, int ipl,
    876     bool mpsafe, int (*intr)(void *), void *iarg, const char *xname)
    877 {
    878 	struct acpi_irq_handler *aih;
    879 	void *ih;
    880 
    881 	const int type = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
    882 	ih = acpi_md_intr_establish(irq->ar_irq, ipl, type, intr, iarg, mpsafe, xname);
    883 	if (ih == NULL)
    884 		return NULL;
    885 
    886 	aih = kmem_alloc(sizeof(struct acpi_irq_handler), KM_SLEEP);
    887 	aih->aih_irq = irq->ar_irq;
    888 	aih->aih_ih = ih;
    889 
    890 	return aih;
    891 }
    892 
    893 void
    894 acpi_intr_mask(void *c)
    895 {
    896 	struct acpi_irq_handler * const aih = c;
    897 
    898 	acpi_md_intr_mask(aih->aih_ih);
    899 }
    900 
    901 void
    902 acpi_intr_unmask(void *c)
    903 {
    904 	struct acpi_irq_handler * const aih = c;
    905 
    906 	acpi_md_intr_unmask(aih->aih_ih);
    907 }
    908 
    909 void
    910 acpi_intr_disestablish(void *c)
    911 {
    912 	struct acpi_irq_handler *aih = c;
    913 
    914 	acpi_md_intr_disestablish(aih->aih_ih);
    915 	kmem_free(aih, sizeof(struct acpi_irq_handler));
    916 }
    917 
    918 const char *
    919 acpi_intr_string(void *c, char *buf, size_t size)
    920 {
    921 	struct acpi_irq_handler *aih = c;
    922 	intr_handle_t ih = aih->aih_irq;
    923 
    924 	return intr_string(ih, buf, size);
    925 }
    926 
    927 /*
    928  * Device-Specific Data (_DSD) support
    929  */
    930 
    931 static UINT8 acpi_dsd_uuid[ACPI_UUID_LENGTH] = {
    932 	0x14, 0xd8, 0xff, 0xda, 0xba, 0x6e, 0x8c, 0x4d,
    933 	0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01
    934 };
    935 
    936 static ACPI_STATUS
    937 acpi_dsd_property(ACPI_HANDLE handle, const char *prop, ACPI_BUFFER *pbuf, ACPI_OBJECT_TYPE type, ACPI_OBJECT **ret)
    938 {
    939 	ACPI_OBJECT *obj, *uuid, *props, *pobj, *propkey, *propval;
    940 	ACPI_STATUS rv;
    941 	int n;
    942 
    943 	rv = AcpiEvaluateObjectTyped(handle, "_DSD", NULL, pbuf, ACPI_TYPE_PACKAGE);
    944 	if (ACPI_FAILURE(rv))
    945 		return rv;
    946 
    947 	props = NULL;
    948 	obj = (ACPI_OBJECT *)pbuf->Pointer;
    949 	for (n = 0; (n + 1) < obj->Package.Count; n += 2) {
    950 		uuid = &obj->Package.Elements[n];
    951 		if (uuid->Buffer.Length == ACPI_UUID_LENGTH &&
    952 		    memcmp(uuid->Buffer.Pointer, acpi_dsd_uuid, ACPI_UUID_LENGTH) == 0) {
    953 			props = &obj->Package.Elements[n + 1];
    954 			break;
    955 		}
    956 	}
    957 	if (props == NULL)
    958 		return AE_NOT_FOUND;
    959 
    960 	for (n = 0; n < props->Package.Count; n++) {
    961 		pobj = &props->Package.Elements[n];
    962 		if (pobj->Type != ACPI_TYPE_PACKAGE || pobj->Package.Count != 2)
    963 			continue;
    964 		propkey = (ACPI_OBJECT *)&pobj->Package.Elements[0];
    965 		propval = (ACPI_OBJECT *)&pobj->Package.Elements[1];
    966 		if (propkey->Type != ACPI_TYPE_STRING)
    967 			continue;
    968 		if (strcmp(propkey->String.Pointer, prop) != 0)
    969 			continue;
    970 
    971 		if (propval->Type != type) {
    972 			return AE_TYPE;
    973 		} else {
    974 			*ret = propval;
    975 			return AE_OK;
    976 		}
    977 		break;
    978 	}
    979 
    980 	return AE_NOT_FOUND;
    981 }
    982 
    983 ACPI_STATUS
    984 acpi_dsd_integer(ACPI_HANDLE handle, const char *prop, ACPI_INTEGER *val)
    985 {
    986 	ACPI_OBJECT *propval;
    987 	ACPI_STATUS rv;
    988 	ACPI_BUFFER buf;
    989 
    990 	buf.Pointer = NULL;
    991 	buf.Length = ACPI_ALLOCATE_BUFFER;
    992 
    993 	rv = acpi_dsd_property(handle, prop, &buf, ACPI_TYPE_INTEGER, &propval);
    994 	if (ACPI_SUCCESS(rv))
    995 		*val = propval->Integer.Value;
    996 
    997 	if (buf.Pointer != NULL)
    998 		ACPI_FREE(buf.Pointer);
    999 	return rv;
   1000 }
   1001 
   1002 ACPI_STATUS
   1003 acpi_dsd_string(ACPI_HANDLE handle, const char *prop, char **val)
   1004 {
   1005 	ACPI_OBJECT *propval;
   1006 	ACPI_STATUS rv;
   1007 	ACPI_BUFFER buf;
   1008 
   1009 	buf.Pointer = NULL;
   1010 	buf.Length = ACPI_ALLOCATE_BUFFER;
   1011 
   1012 	rv = acpi_dsd_property(handle, prop, &buf, ACPI_TYPE_STRING, &propval);
   1013 	if (ACPI_SUCCESS(rv))
   1014 		*val = kmem_strdup(propval->String.Pointer, KM_SLEEP);
   1015 
   1016 	if (buf.Pointer != NULL)
   1017 		ACPI_FREE(buf.Pointer);
   1018 	return rv;
   1019 }
   1020 
   1021 
   1022 
   1023 /*
   1024  * Device Specific Method (_DSM) support
   1025  */
   1026 
   1027 ACPI_STATUS
   1028 acpi_dsm_typed(ACPI_HANDLE handle, uint8_t *uuid, ACPI_INTEGER rev,
   1029     ACPI_INTEGER func, const ACPI_OBJECT *arg3, ACPI_OBJECT_TYPE return_type,
   1030     ACPI_OBJECT **return_obj)
   1031 {
   1032 	ACPI_OBJECT_LIST arg;
   1033 	ACPI_OBJECT obj[4];
   1034 	ACPI_BUFFER buf;
   1035 	ACPI_STATUS status;
   1036 
   1037 	arg.Count = 4;
   1038 	arg.Pointer = obj;
   1039 
   1040 	obj[0].Type = ACPI_TYPE_BUFFER;
   1041 	obj[0].Buffer.Length = ACPI_UUID_LENGTH;
   1042 	obj[0].Buffer.Pointer = uuid;
   1043 
   1044 	obj[1].Type = ACPI_TYPE_INTEGER;
   1045 	obj[1].Integer.Value = rev;
   1046 
   1047 	obj[2].Type = ACPI_TYPE_INTEGER;
   1048 	obj[2].Integer.Value = func;
   1049 
   1050 	if (arg3 != NULL) {
   1051 		obj[3] = *arg3;
   1052 	} else {
   1053 		obj[3].Type = ACPI_TYPE_PACKAGE;
   1054 		obj[3].Package.Count = 0;
   1055 		obj[3].Package.Elements = NULL;
   1056 	}
   1057 
   1058 	buf.Pointer = NULL;
   1059 	buf.Length = ACPI_ALLOCATE_BUFFER;
   1060 
   1061 	if (return_obj == NULL && return_type == ACPI_TYPE_ANY) {
   1062 		status = AcpiEvaluateObject(handle, "_DSM", &arg, NULL);
   1063 	} else {
   1064 		*return_obj = NULL;
   1065 		status = AcpiEvaluateObjectTyped(handle, "_DSM", &arg, &buf,
   1066 		    return_type);
   1067 	}
   1068 	if (ACPI_FAILURE(status)) {
   1069 		return status;
   1070 	}
   1071 	if (return_obj != NULL) {
   1072 		*return_obj = buf.Pointer;
   1073 	} else if (buf.Pointer != NULL) {
   1074 		ACPI_FREE(buf.Pointer);
   1075 	}
   1076 	return AE_OK;
   1077 }
   1078 
   1079 ACPI_STATUS
   1080 acpi_dsm_integer(ACPI_HANDLE handle, uint8_t *uuid, ACPI_INTEGER rev,
   1081     ACPI_INTEGER func, const ACPI_OBJECT *arg3, ACPI_INTEGER *ret)
   1082 {
   1083 	ACPI_OBJECT *obj;
   1084 	ACPI_STATUS status;
   1085 
   1086 	status = acpi_dsm_typed(handle, uuid, rev, func, arg3,
   1087 	    ACPI_TYPE_INTEGER, &obj);
   1088 	if (ACPI_FAILURE(status)) {
   1089 		return status;
   1090 	}
   1091 
   1092 	*ret = obj->Integer.Value;
   1093 	ACPI_FREE(obj);
   1094 
   1095 	return AE_OK;
   1096 }
   1097 
   1098 ACPI_STATUS
   1099 acpi_dsm(ACPI_HANDLE handle, uint8_t *uuid, ACPI_INTEGER rev,
   1100     ACPI_INTEGER func, const ACPI_OBJECT *arg3, ACPI_OBJECT **return_obj)
   1101 {
   1102 	return acpi_dsm_typed(handle, uuid, rev, func, arg3, ACPI_TYPE_ANY,
   1103 	    return_obj);
   1104 }
   1105 
   1106 ACPI_STATUS
   1107 acpi_claim_childdevs(device_t dev, struct acpi_devnode *devnode)
   1108 {
   1109 	struct acpi_devnode *ad;
   1110 
   1111 	SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
   1112 		if (ad->ad_device != NULL)
   1113 			continue;
   1114 		aprint_debug_dev(dev, "claiming %s\n",
   1115 		    acpi_name(ad->ad_handle));
   1116 		ad->ad_device = dev;
   1117 		acpi_claim_childdevs(dev, ad);
   1118 	}
   1119 
   1120 	return AE_OK;
   1121 }
   1122