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