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