Home | History | Annotate | Line # | Download | only in acpi
acpi_util.c revision 1.14
      1  1.14  jmcneill /*	$NetBSD: acpi_util.c,v 1.14 2018/11/16 23:05:50 jmcneill Exp $ */
      2   1.1    jruoho 
      3   1.1    jruoho /*-
      4   1.1    jruoho  * Copyright (c) 2003, 2007 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.14  jmcneill __KERNEL_RCSID(0, "$NetBSD: acpi_util.c,v 1.14 2018/11/16 23:05:50 jmcneill 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.2    jruoho  * Evaluate an integer object.
     92   1.1    jruoho  */
     93   1.1    jruoho ACPI_STATUS
     94   1.1    jruoho acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
     95   1.1    jruoho {
     96   1.1    jruoho 	ACPI_OBJECT obj;
     97   1.1    jruoho 	ACPI_BUFFER buf;
     98   1.1    jruoho 	ACPI_STATUS rv;
     99   1.1    jruoho 
    100   1.1    jruoho 	if (handle == NULL)
    101   1.1    jruoho 		handle = ACPI_ROOT_OBJECT;
    102   1.1    jruoho 
    103   1.6    gsutre 	(void)memset(&obj, 0, sizeof(obj));
    104   1.1    jruoho 	buf.Pointer = &obj;
    105   1.1    jruoho 	buf.Length = sizeof(obj);
    106   1.1    jruoho 
    107   1.1    jruoho 	rv = AcpiEvaluateObject(handle, path, NULL, &buf);
    108   1.1    jruoho 
    109   1.1    jruoho 	if (ACPI_FAILURE(rv))
    110   1.1    jruoho 		return rv;
    111   1.1    jruoho 
    112   1.6    gsutre 	/* Check that evaluation produced a return value. */
    113   1.6    gsutre 	if (buf.Length == 0)
    114   1.6    gsutre 		return AE_NULL_OBJECT;
    115   1.6    gsutre 
    116   1.1    jruoho 	if (obj.Type != ACPI_TYPE_INTEGER)
    117   1.1    jruoho 		return AE_TYPE;
    118   1.1    jruoho 
    119   1.1    jruoho 	if (valp != NULL)
    120   1.1    jruoho 		*valp = obj.Integer.Value;
    121   1.1    jruoho 
    122   1.1    jruoho 	return AE_OK;
    123   1.1    jruoho }
    124   1.1    jruoho 
    125   1.1    jruoho /*
    126   1.2    jruoho  * Evaluate an integer object with a single integer input parameter.
    127   1.1    jruoho  */
    128   1.1    jruoho ACPI_STATUS
    129   1.1    jruoho acpi_eval_set_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER val)
    130   1.1    jruoho {
    131   1.1    jruoho 	ACPI_OBJECT_LIST arg;
    132   1.1    jruoho 	ACPI_OBJECT obj;
    133   1.1    jruoho 
    134   1.1    jruoho 	if (handle == NULL)
    135   1.1    jruoho 		handle = ACPI_ROOT_OBJECT;
    136   1.1    jruoho 
    137   1.1    jruoho 	obj.Type = ACPI_TYPE_INTEGER;
    138   1.1    jruoho 	obj.Integer.Value = val;
    139   1.1    jruoho 
    140   1.1    jruoho 	arg.Count = 1;
    141   1.1    jruoho 	arg.Pointer = &obj;
    142   1.1    jruoho 
    143   1.1    jruoho 	return AcpiEvaluateObject(handle, path, &arg, NULL);
    144   1.1    jruoho }
    145   1.1    jruoho 
    146   1.1    jruoho /*
    147   1.2    jruoho  * Evaluate a (Unicode) string object.
    148   1.1    jruoho  */
    149   1.1    jruoho ACPI_STATUS
    150   1.1    jruoho acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
    151   1.1    jruoho {
    152   1.1    jruoho 	ACPI_OBJECT *obj;
    153   1.1    jruoho 	ACPI_BUFFER buf;
    154   1.1    jruoho 	ACPI_STATUS rv;
    155   1.1    jruoho 
    156   1.1    jruoho 	rv = acpi_eval_struct(handle, path, &buf);
    157   1.1    jruoho 
    158   1.1    jruoho 	if (ACPI_FAILURE(rv))
    159   1.1    jruoho 		return rv;
    160   1.1    jruoho 
    161   1.1    jruoho 	obj = buf.Pointer;
    162   1.1    jruoho 
    163   1.1    jruoho 	if (obj->Type != ACPI_TYPE_STRING) {
    164   1.1    jruoho 		rv = AE_TYPE;
    165   1.1    jruoho 		goto out;
    166   1.1    jruoho 	}
    167   1.1    jruoho 
    168   1.1    jruoho 	if (obj->String.Length == 0) {
    169   1.1    jruoho 		rv = AE_BAD_DATA;
    170   1.1    jruoho 		goto out;
    171   1.1    jruoho 	}
    172   1.1    jruoho 
    173   1.1    jruoho 	*stringp = ACPI_ALLOCATE(obj->String.Length + 1);
    174   1.1    jruoho 
    175   1.1    jruoho 	if (*stringp == NULL) {
    176   1.1    jruoho 		rv = AE_NO_MEMORY;
    177   1.1    jruoho 		goto out;
    178   1.1    jruoho 	}
    179   1.1    jruoho 
    180   1.1    jruoho 	(void)memcpy(*stringp, obj->String.Pointer, obj->String.Length);
    181   1.1    jruoho 
    182   1.1    jruoho 	(*stringp)[obj->String.Length] = '\0';
    183   1.1    jruoho 
    184   1.1    jruoho out:
    185   1.1    jruoho 	ACPI_FREE(buf.Pointer);
    186   1.1    jruoho 
    187   1.1    jruoho 	return rv;
    188   1.1    jruoho }
    189   1.1    jruoho 
    190   1.1    jruoho /*
    191   1.2    jruoho  * Evaluate a structure. Caller must free buf.Pointer by ACPI_FREE().
    192   1.1    jruoho  */
    193   1.1    jruoho ACPI_STATUS
    194   1.1    jruoho acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *buf)
    195   1.1    jruoho {
    196   1.1    jruoho 
    197   1.1    jruoho 	if (handle == NULL)
    198   1.1    jruoho 		handle = ACPI_ROOT_OBJECT;
    199   1.1    jruoho 
    200   1.1    jruoho 	buf->Pointer = NULL;
    201   1.1    jruoho 	buf->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    202   1.1    jruoho 
    203   1.1    jruoho 	return AcpiEvaluateObject(handle, path, NULL, buf);
    204   1.1    jruoho }
    205   1.1    jruoho 
    206   1.1    jruoho /*
    207   1.2    jruoho  * Evaluate a reference handle from an element in a package.
    208   1.1    jruoho  */
    209   1.1    jruoho ACPI_STATUS
    210   1.1    jruoho acpi_eval_reference_handle(ACPI_OBJECT *elm, ACPI_HANDLE *handle)
    211   1.1    jruoho {
    212   1.1    jruoho 
    213   1.1    jruoho 	if (elm == NULL || handle == NULL)
    214   1.1    jruoho 		return AE_BAD_PARAMETER;
    215   1.1    jruoho 
    216   1.1    jruoho 	switch (elm->Type) {
    217   1.1    jruoho 
    218   1.1    jruoho 	case ACPI_TYPE_ANY:
    219   1.1    jruoho 	case ACPI_TYPE_LOCAL_REFERENCE:
    220   1.1    jruoho 
    221   1.1    jruoho 		if (elm->Reference.Handle == NULL)
    222   1.1    jruoho 			return AE_NULL_ENTRY;
    223   1.1    jruoho 
    224   1.1    jruoho 		*handle = elm->Reference.Handle;
    225   1.1    jruoho 
    226   1.1    jruoho 		return AE_OK;
    227   1.1    jruoho 
    228   1.1    jruoho 	case ACPI_TYPE_STRING:
    229   1.1    jruoho 		return AcpiGetHandle(NULL, elm->String.Pointer, handle);
    230   1.1    jruoho 
    231   1.1    jruoho 	default:
    232   1.1    jruoho 		return AE_TYPE;
    233   1.1    jruoho 	}
    234   1.1    jruoho }
    235   1.1    jruoho 
    236   1.1    jruoho /*
    237   1.2    jruoho  * Iterate over all objects in a package, and pass them all
    238   1.2    jruoho  * to a function. If the called function returns non-AE_OK,
    239   1.2    jruoho  * the iteration is stopped and that value is returned.
    240   1.1    jruoho  */
    241   1.1    jruoho ACPI_STATUS
    242   1.1    jruoho acpi_foreach_package_object(ACPI_OBJECT *pkg,
    243   1.1    jruoho     ACPI_STATUS (*func)(ACPI_OBJECT *, void *), void *arg)
    244   1.1    jruoho {
    245   1.1    jruoho 	ACPI_STATUS rv = AE_OK;
    246   1.1    jruoho 	uint32_t i;
    247   1.1    jruoho 
    248   1.4    jruoho 	if (pkg == NULL)
    249   1.1    jruoho 		return AE_BAD_PARAMETER;
    250   1.1    jruoho 
    251   1.4    jruoho 	if (pkg->Type != ACPI_TYPE_PACKAGE)
    252   1.4    jruoho 		return AE_TYPE;
    253   1.4    jruoho 
    254   1.1    jruoho 	for (i = 0; i < pkg->Package.Count; i++) {
    255   1.1    jruoho 
    256   1.1    jruoho 		rv = (*func)(&pkg->Package.Elements[i], arg);
    257   1.1    jruoho 
    258   1.1    jruoho 		if (ACPI_FAILURE(rv))
    259   1.1    jruoho 			break;
    260   1.1    jruoho 	}
    261   1.1    jruoho 
    262   1.1    jruoho 	return rv;
    263   1.1    jruoho }
    264   1.1    jruoho 
    265   1.1    jruoho /*
    266   1.2    jruoho  * Fetch data info the specified (empty) ACPI buffer.
    267   1.2    jruoho  * Caller must free buf.Pointer by ACPI_FREE().
    268   1.1    jruoho  */
    269   1.1    jruoho ACPI_STATUS
    270   1.1    jruoho acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
    271   1.1    jruoho     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
    272   1.1    jruoho {
    273   1.1    jruoho 
    274   1.1    jruoho 	buf->Pointer = NULL;
    275   1.1    jruoho 	buf->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    276   1.1    jruoho 
    277   1.1    jruoho 	return (*getit)(handle, buf);
    278   1.1    jruoho }
    279   1.1    jruoho 
    280   1.1    jruoho /*
    281   1.2    jruoho  * Return a complete pathname from a handle.
    282   1.1    jruoho  *
    283   1.2    jruoho  * Note that the function uses static data storage;
    284   1.2    jruoho  * if the data is needed for future use, it should be
    285   1.2    jruoho  * copied before any subsequent calls overwrite it.
    286   1.1    jruoho  */
    287   1.1    jruoho const char *
    288   1.1    jruoho acpi_name(ACPI_HANDLE handle)
    289   1.1    jruoho {
    290   1.1    jruoho 	static char name[80];
    291   1.1    jruoho 	ACPI_BUFFER buf;
    292   1.1    jruoho 	ACPI_STATUS rv;
    293   1.1    jruoho 
    294   1.4    jruoho 	if (handle == NULL)
    295   1.4    jruoho 		handle = ACPI_ROOT_OBJECT;
    296   1.4    jruoho 
    297   1.1    jruoho 	buf.Pointer = name;
    298   1.1    jruoho 	buf.Length = sizeof(name);
    299   1.1    jruoho 
    300   1.1    jruoho 	rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
    301   1.1    jruoho 
    302   1.1    jruoho 	if (ACPI_FAILURE(rv))
    303   1.1    jruoho 		return "UNKNOWN";
    304   1.1    jruoho 
    305   1.1    jruoho 	return name;
    306   1.1    jruoho }
    307   1.1    jruoho 
    308   1.1    jruoho /*
    309   1.2    jruoho  * Match given IDs against _HID and _CIDs.
    310   1.1    jruoho  */
    311   1.1    jruoho int
    312   1.1    jruoho acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
    313   1.1    jruoho {
    314   1.1    jruoho 	uint32_t i, n;
    315   1.1    jruoho 	char *id;
    316   1.1    jruoho 
    317   1.1    jruoho 	while (*ids) {
    318   1.1    jruoho 
    319   1.1    jruoho 		if ((ad->Valid & ACPI_VALID_HID) != 0) {
    320   1.1    jruoho 
    321   1.1    jruoho 			if (pmatch(ad->HardwareId.String, *ids, NULL) == 2)
    322   1.1    jruoho 				return 1;
    323   1.1    jruoho 		}
    324   1.1    jruoho 
    325   1.1    jruoho 		if ((ad->Valid & ACPI_VALID_CID) != 0) {
    326   1.1    jruoho 
    327   1.1    jruoho 			n = ad->CompatibleIdList.Count;
    328   1.1    jruoho 
    329   1.1    jruoho 			for (i = 0; i < n; i++) {
    330   1.1    jruoho 
    331   1.1    jruoho 				id = ad->CompatibleIdList.Ids[i].String;
    332   1.1    jruoho 
    333   1.1    jruoho 				if (pmatch(id, *ids, NULL) == 2)
    334   1.1    jruoho 					return 1;
    335   1.1    jruoho 			}
    336   1.1    jruoho 		}
    337   1.1    jruoho 
    338   1.1    jruoho 		ids++;
    339   1.1    jruoho 	}
    340   1.1    jruoho 
    341   1.1    jruoho 	return 0;
    342   1.1    jruoho }
    343   1.1    jruoho 
    344   1.7    jruoho /*
    345  1.13  jmcneill  * Match a PCI-defined bass-class, sub-class, and programming interface
    346  1.13  jmcneill  * against a handle's _CLS object.
    347  1.13  jmcneill  */
    348  1.13  jmcneill int
    349  1.13  jmcneill acpi_match_class(ACPI_HANDLE handle, uint8_t pci_class, uint8_t pci_subclass,
    350  1.13  jmcneill     uint8_t pci_interface)
    351  1.13  jmcneill {
    352  1.13  jmcneill 	ACPI_BUFFER buf;
    353  1.13  jmcneill 	ACPI_OBJECT *obj;
    354  1.13  jmcneill 	ACPI_STATUS rv;
    355  1.13  jmcneill 	int match = 0;
    356  1.13  jmcneill 
    357  1.13  jmcneill 	rv = acpi_eval_struct(handle, "_CLS", &buf);
    358  1.13  jmcneill 	if (ACPI_FAILURE(rv))
    359  1.13  jmcneill 		goto done;
    360  1.13  jmcneill 
    361  1.13  jmcneill 	obj = buf.Pointer;
    362  1.13  jmcneill 	if (obj->Type != ACPI_TYPE_PACKAGE)
    363  1.13  jmcneill 		goto done;
    364  1.13  jmcneill 	if (obj->Package.Count != 3)
    365  1.13  jmcneill 		goto done;
    366  1.13  jmcneill 	if (obj->Package.Elements[0].Type != ACPI_TYPE_INTEGER ||
    367  1.13  jmcneill 	    obj->Package.Elements[1].Type != ACPI_TYPE_INTEGER ||
    368  1.13  jmcneill 	    obj->Package.Elements[2].Type != ACPI_TYPE_INTEGER)
    369  1.13  jmcneill 		goto done;
    370  1.13  jmcneill 
    371  1.13  jmcneill 	match = obj->Package.Elements[0].Integer.Value == pci_class &&
    372  1.13  jmcneill 		obj->Package.Elements[1].Integer.Value == pci_subclass &&
    373  1.13  jmcneill 		obj->Package.Elements[2].Integer.Value == pci_interface;
    374  1.13  jmcneill 
    375  1.13  jmcneill done:
    376  1.13  jmcneill 	if (buf.Pointer)
    377  1.13  jmcneill 		ACPI_FREE(buf.Pointer);
    378  1.13  jmcneill 	return match;
    379  1.13  jmcneill }
    380  1.13  jmcneill 
    381  1.13  jmcneill /*
    382   1.8    jruoho  * Match a device node from a handle.
    383   1.8    jruoho  */
    384   1.8    jruoho struct acpi_devnode *
    385   1.8    jruoho acpi_match_node(ACPI_HANDLE handle)
    386   1.8    jruoho {
    387   1.8    jruoho 	struct acpi_devnode *ad;
    388   1.8    jruoho 	ACPI_STATUS rv;
    389   1.8    jruoho 
    390   1.8    jruoho 	if (handle == NULL)
    391   1.8    jruoho 		return NULL;
    392   1.8    jruoho 
    393   1.8    jruoho 	rv = AcpiGetData(handle, acpi_clean_node, (void **)&ad);
    394   1.8    jruoho 
    395   1.8    jruoho 	if (ACPI_FAILURE(rv))
    396   1.8    jruoho 		return NULL;
    397   1.8    jruoho 
    398   1.8    jruoho 	return ad;
    399   1.8    jruoho }
    400   1.8    jruoho 
    401   1.8    jruoho /*
    402   1.8    jruoho  * Permanently associate a device node with a handle.
    403   1.8    jruoho  */
    404   1.8    jruoho void
    405   1.8    jruoho acpi_match_node_init(struct acpi_devnode *ad)
    406   1.8    jruoho {
    407   1.8    jruoho 	(void)AcpiAttachData(ad->ad_handle, acpi_clean_node, ad);
    408   1.8    jruoho }
    409   1.8    jruoho 
    410   1.8    jruoho static void
    411   1.8    jruoho acpi_clean_node(ACPI_HANDLE handle, void *aux)
    412   1.8    jruoho {
    413   1.8    jruoho 	/* Nothing. */
    414   1.8    jruoho }
    415   1.8    jruoho 
    416   1.8    jruoho /*
    417   1.7    jruoho  * Match a handle from a cpu_info. Returns NULL on failure.
    418   1.7    jruoho  *
    419   1.8    jruoho  * Note that acpi_match_node() can be used if the device node
    420   1.8    jruoho  * is also required.
    421   1.7    jruoho  */
    422   1.7    jruoho ACPI_HANDLE
    423   1.7    jruoho acpi_match_cpu_info(struct cpu_info *ci)
    424   1.7    jruoho {
    425   1.7    jruoho 	struct acpi_softc *sc = acpi_softc;
    426   1.7    jruoho 	struct acpi_devnode *ad;
    427   1.7    jruoho 	ACPI_INTEGER val;
    428   1.7    jruoho 	ACPI_OBJECT *obj;
    429   1.7    jruoho 	ACPI_BUFFER buf;
    430   1.7    jruoho 	ACPI_HANDLE hdl;
    431   1.7    jruoho 	ACPI_STATUS rv;
    432   1.7    jruoho 
    433   1.7    jruoho 	if (sc == NULL || acpi_active == 0)
    434   1.7    jruoho 		return NULL;
    435   1.7    jruoho 
    436   1.7    jruoho 	/*
    437   1.7    jruoho 	 * CPUs are declared in the ACPI namespace
    438   1.7    jruoho 	 * either as a Processor() or as a Device().
    439   1.7    jruoho 	 * In both cases the MADT entries are used
    440   1.7    jruoho 	 * for the match (see ACPI 4.0, section 8.4).
    441   1.7    jruoho 	 */
    442   1.7    jruoho 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    443   1.7    jruoho 
    444   1.7    jruoho 		hdl = ad->ad_handle;
    445   1.7    jruoho 
    446   1.7    jruoho 		switch (ad->ad_type) {
    447   1.7    jruoho 
    448   1.7    jruoho 		case ACPI_TYPE_DEVICE:
    449   1.7    jruoho 
    450   1.7    jruoho 			if (acpi_match_hid(ad->ad_devinfo, acpicpu_ids) == 0)
    451   1.7    jruoho 				break;
    452   1.7    jruoho 
    453   1.7    jruoho 			rv = acpi_eval_integer(hdl, "_UID", &val);
    454   1.7    jruoho 
    455   1.7    jruoho 			if (ACPI_SUCCESS(rv) && val == ci->ci_acpiid)
    456   1.7    jruoho 				return hdl;
    457   1.7    jruoho 
    458   1.7    jruoho 			break;
    459   1.7    jruoho 
    460   1.7    jruoho 		case ACPI_TYPE_PROCESSOR:
    461   1.7    jruoho 
    462   1.7    jruoho 			rv = acpi_eval_struct(hdl, NULL, &buf);
    463   1.7    jruoho 
    464   1.7    jruoho 			if (ACPI_FAILURE(rv))
    465   1.7    jruoho 				break;
    466   1.7    jruoho 
    467   1.7    jruoho 			obj = buf.Pointer;
    468   1.7    jruoho 
    469   1.7    jruoho 			if (obj->Processor.ProcId == ci->ci_acpiid) {
    470   1.7    jruoho 				ACPI_FREE(buf.Pointer);
    471   1.7    jruoho 				return hdl;
    472   1.7    jruoho 			}
    473   1.7    jruoho 
    474   1.7    jruoho 			ACPI_FREE(buf.Pointer);
    475   1.7    jruoho 			break;
    476   1.7    jruoho 		}
    477   1.7    jruoho 	}
    478   1.7    jruoho 
    479   1.7    jruoho 	return NULL;
    480   1.7    jruoho }
    481   1.7    jruoho 
    482   1.7    jruoho /*
    483   1.7    jruoho  * Match a CPU from a handle. Returns NULL on failure.
    484   1.7    jruoho  */
    485   1.7    jruoho struct cpu_info *
    486   1.7    jruoho acpi_match_cpu_handle(ACPI_HANDLE hdl)
    487   1.7    jruoho {
    488   1.7    jruoho 	struct cpu_info *ci;
    489   1.7    jruoho 	ACPI_DEVICE_INFO *di;
    490   1.7    jruoho 	CPU_INFO_ITERATOR cii;
    491   1.7    jruoho 	ACPI_INTEGER val;
    492   1.7    jruoho 	ACPI_OBJECT *obj;
    493   1.7    jruoho 	ACPI_BUFFER buf;
    494   1.7    jruoho 	ACPI_STATUS rv;
    495   1.7    jruoho 
    496   1.7    jruoho 	ci = NULL;
    497   1.7    jruoho 	di = NULL;
    498   1.7    jruoho 	buf.Pointer = NULL;
    499   1.7    jruoho 
    500   1.7    jruoho 	rv = AcpiGetObjectInfo(hdl, &di);
    501   1.7    jruoho 
    502   1.7    jruoho 	if (ACPI_FAILURE(rv))
    503   1.7    jruoho 		return NULL;
    504   1.7    jruoho 
    505   1.7    jruoho 	switch (di->Type) {
    506   1.7    jruoho 
    507   1.7    jruoho 	case ACPI_TYPE_DEVICE:
    508   1.7    jruoho 
    509   1.7    jruoho 		if (acpi_match_hid(di, acpicpu_ids) == 0)
    510   1.7    jruoho 			goto out;
    511   1.7    jruoho 
    512   1.7    jruoho 		rv = acpi_eval_integer(hdl, "_UID", &val);
    513   1.7    jruoho 
    514   1.7    jruoho 		if (ACPI_FAILURE(rv))
    515   1.7    jruoho 			goto out;
    516   1.7    jruoho 
    517   1.7    jruoho 		break;
    518   1.7    jruoho 
    519   1.7    jruoho 	case ACPI_TYPE_PROCESSOR:
    520   1.7    jruoho 
    521   1.7    jruoho 		rv = acpi_eval_struct(hdl, NULL, &buf);
    522   1.7    jruoho 
    523   1.7    jruoho 		if (ACPI_FAILURE(rv))
    524   1.7    jruoho 			goto out;
    525   1.7    jruoho 
    526   1.7    jruoho 		obj = buf.Pointer;
    527   1.7    jruoho 		val = obj->Processor.ProcId;
    528   1.7    jruoho 		break;
    529   1.7    jruoho 
    530   1.7    jruoho 	default:
    531   1.7    jruoho 		goto out;
    532   1.7    jruoho 	}
    533   1.7    jruoho 
    534   1.7    jruoho 	for (CPU_INFO_FOREACH(cii, ci)) {
    535   1.7    jruoho 
    536   1.7    jruoho 		if (ci->ci_acpiid == val)
    537   1.7    jruoho 			goto out;
    538   1.7    jruoho 	}
    539   1.7    jruoho 
    540   1.7    jruoho 	ci = NULL;
    541   1.7    jruoho 
    542   1.7    jruoho out:
    543   1.7    jruoho 	if (di != NULL)
    544   1.7    jruoho 		ACPI_FREE(di);
    545   1.7    jruoho 
    546   1.7    jruoho 	if (buf.Pointer != NULL)
    547   1.7    jruoho 		ACPI_FREE(buf.Pointer);
    548   1.7    jruoho 
    549   1.7    jruoho 	return ci;
    550   1.7    jruoho }
    551   1.9    bouyer 
    552   1.9    bouyer struct acpi_irq_handler {
    553   1.9    bouyer 	ACPI_HANDLE aih_hdl;
    554   1.9    bouyer 	uint32_t aih_irq;
    555  1.14  jmcneill 	void *aih_ih;
    556   1.9    bouyer };
    557   1.9    bouyer 
    558   1.9    bouyer void *
    559  1.14  jmcneill acpi_intr_establish(device_t dev, uint64_t c, int ipl, bool mpsafe,
    560  1.14  jmcneill     int (*intr)(void *), void *iarg, const char *xname)
    561   1.9    bouyer {
    562   1.9    bouyer 	ACPI_STATUS rv;
    563  1.10    bouyer 	ACPI_HANDLE hdl = (void *)(uintptr_t)c;
    564   1.9    bouyer 	struct acpi_resources res;
    565   1.9    bouyer 	struct acpi_irq *irq;
    566   1.9    bouyer 	struct acpi_irq_handler *aih = NULL;
    567  1.14  jmcneill 	void *ih;
    568   1.9    bouyer 
    569   1.9    bouyer 	rv = acpi_resource_parse(dev, hdl, "_CRS", &res,
    570   1.9    bouyer 	    &acpi_resource_parse_ops_quiet);
    571   1.9    bouyer 	if (ACPI_FAILURE(rv))
    572   1.9    bouyer 		return NULL;
    573   1.9    bouyer 
    574   1.9    bouyer 	irq = acpi_res_irq(&res, 0);
    575   1.9    bouyer 	if (irq == NULL)
    576   1.9    bouyer 		goto end;
    577   1.9    bouyer 
    578  1.14  jmcneill 	const int type = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
    579  1.14  jmcneill 	ih = acpi_md_intr_establish(irq->ar_irq, ipl, type, intr, iarg, mpsafe, xname);
    580  1.14  jmcneill 	if (ih == NULL)
    581   1.9    bouyer 		goto end;
    582   1.9    bouyer 
    583  1.14  jmcneill 	aih = kmem_alloc(sizeof(struct acpi_irq_handler), KM_SLEEP);
    584   1.9    bouyer 	aih->aih_hdl = hdl;
    585   1.9    bouyer 	aih->aih_irq = irq->ar_irq;
    586  1.14  jmcneill 	aih->aih_ih = ih;
    587  1.14  jmcneill 
    588   1.9    bouyer end:
    589   1.9    bouyer 	acpi_resource_cleanup(&res);
    590   1.9    bouyer 	return aih;
    591   1.9    bouyer }
    592   1.9    bouyer 
    593   1.9    bouyer void
    594  1.14  jmcneill acpi_intr_disestablish(void *c)
    595   1.9    bouyer {
    596   1.9    bouyer 	struct acpi_irq_handler *aih = c;
    597   1.9    bouyer 
    598  1.14  jmcneill 	acpi_md_intr_disestablish(aih->aih_ih);
    599   1.9    bouyer 	kmem_free(aih, sizeof(struct acpi_irq_handler));
    600   1.9    bouyer }
    601   1.9    bouyer 
    602   1.9    bouyer const char *
    603   1.9    bouyer acpi_intr_string(void *c, char *buf, size_t size)
    604   1.9    bouyer {
    605   1.9    bouyer 	struct acpi_irq_handler *aih = c;
    606   1.9    bouyer 	intr_handle_t ih = aih->aih_irq;
    607   1.9    bouyer 
    608   1.9    bouyer 	return intr_string(ih, buf, size);
    609   1.9    bouyer }
    610