Home | History | Annotate | Line # | Download | only in acpi
acpi_util.c revision 1.10.2.2
      1 /*	$NetBSD: acpi_util.c,v 1.10.2.2 2018/10/20 06:58:30 pgoyette Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2003, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum of By Noon Software, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright 2001, 2003 Wasabi Systems, Inc.
     34  * All rights reserved.
     35  *
     36  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. All advertising materials mentioning features or use of this software
     47  *    must display the following acknowledgement:
     48  *	This product includes software developed for the NetBSD Project by
     49  *	Wasabi Systems, Inc.
     50  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     51  *    or promote products derived from this software without specific prior
     52  *    written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     57  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     58  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     59  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     60  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     61  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     62  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     63  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     64  * POSSIBILITY OF SUCH DAMAGE.
     65  */
     66 
     67 #include <sys/cdefs.h>
     68 __KERNEL_RCSID(0, "$NetBSD: acpi_util.c,v 1.10.2.2 2018/10/20 06:58:30 pgoyette Exp $");
     69 
     70 #include <sys/param.h>
     71 #include <sys/kmem.h>
     72 #include <sys/cpu.h>
     73 
     74 #include <dev/acpi/acpireg.h>
     75 #include <dev/acpi/acpivar.h>
     76 #include <dev/acpi/acpi_intr.h>
     77 
     78 #define _COMPONENT	ACPI_BUS_COMPONENT
     79 ACPI_MODULE_NAME	("acpi_util")
     80 
     81 static void		acpi_clean_node(ACPI_HANDLE, void *);
     82 
     83 static const char * const acpicpu_ids[] = {
     84 	"ACPI0007",
     85 	NULL
     86 };
     87 
     88 /*
     89  * Evaluate an integer object.
     90  */
     91 ACPI_STATUS
     92 acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
     93 {
     94 	ACPI_OBJECT obj;
     95 	ACPI_BUFFER buf;
     96 	ACPI_STATUS rv;
     97 
     98 	if (handle == NULL)
     99 		handle = ACPI_ROOT_OBJECT;
    100 
    101 	(void)memset(&obj, 0, sizeof(obj));
    102 	buf.Pointer = &obj;
    103 	buf.Length = sizeof(obj);
    104 
    105 	rv = AcpiEvaluateObject(handle, path, NULL, &buf);
    106 
    107 	if (ACPI_FAILURE(rv))
    108 		return rv;
    109 
    110 	/* Check that evaluation produced a return value. */
    111 	if (buf.Length == 0)
    112 		return AE_NULL_OBJECT;
    113 
    114 	if (obj.Type != ACPI_TYPE_INTEGER)
    115 		return AE_TYPE;
    116 
    117 	if (valp != NULL)
    118 		*valp = obj.Integer.Value;
    119 
    120 	return AE_OK;
    121 }
    122 
    123 /*
    124  * Evaluate an integer object with a single integer input parameter.
    125  */
    126 ACPI_STATUS
    127 acpi_eval_set_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER val)
    128 {
    129 	ACPI_OBJECT_LIST arg;
    130 	ACPI_OBJECT obj;
    131 
    132 	if (handle == NULL)
    133 		handle = ACPI_ROOT_OBJECT;
    134 
    135 	obj.Type = ACPI_TYPE_INTEGER;
    136 	obj.Integer.Value = val;
    137 
    138 	arg.Count = 1;
    139 	arg.Pointer = &obj;
    140 
    141 	return AcpiEvaluateObject(handle, path, &arg, NULL);
    142 }
    143 
    144 /*
    145  * Evaluate a (Unicode) string object.
    146  */
    147 ACPI_STATUS
    148 acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
    149 {
    150 	ACPI_OBJECT *obj;
    151 	ACPI_BUFFER buf;
    152 	ACPI_STATUS rv;
    153 
    154 	rv = acpi_eval_struct(handle, path, &buf);
    155 
    156 	if (ACPI_FAILURE(rv))
    157 		return rv;
    158 
    159 	obj = buf.Pointer;
    160 
    161 	if (obj->Type != ACPI_TYPE_STRING) {
    162 		rv = AE_TYPE;
    163 		goto out;
    164 	}
    165 
    166 	if (obj->String.Length == 0) {
    167 		rv = AE_BAD_DATA;
    168 		goto out;
    169 	}
    170 
    171 	*stringp = ACPI_ALLOCATE(obj->String.Length + 1);
    172 
    173 	if (*stringp == NULL) {
    174 		rv = AE_NO_MEMORY;
    175 		goto out;
    176 	}
    177 
    178 	(void)memcpy(*stringp, obj->String.Pointer, obj->String.Length);
    179 
    180 	(*stringp)[obj->String.Length] = '\0';
    181 
    182 out:
    183 	ACPI_FREE(buf.Pointer);
    184 
    185 	return rv;
    186 }
    187 
    188 /*
    189  * Evaluate a structure. Caller must free buf.Pointer by ACPI_FREE().
    190  */
    191 ACPI_STATUS
    192 acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *buf)
    193 {
    194 
    195 	if (handle == NULL)
    196 		handle = ACPI_ROOT_OBJECT;
    197 
    198 	buf->Pointer = NULL;
    199 	buf->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    200 
    201 	return AcpiEvaluateObject(handle, path, NULL, buf);
    202 }
    203 
    204 /*
    205  * Evaluate a reference handle from an element in a package.
    206  */
    207 ACPI_STATUS
    208 acpi_eval_reference_handle(ACPI_OBJECT *elm, ACPI_HANDLE *handle)
    209 {
    210 
    211 	if (elm == NULL || handle == NULL)
    212 		return AE_BAD_PARAMETER;
    213 
    214 	switch (elm->Type) {
    215 
    216 	case ACPI_TYPE_ANY:
    217 	case ACPI_TYPE_LOCAL_REFERENCE:
    218 
    219 		if (elm->Reference.Handle == NULL)
    220 			return AE_NULL_ENTRY;
    221 
    222 		*handle = elm->Reference.Handle;
    223 
    224 		return AE_OK;
    225 
    226 	case ACPI_TYPE_STRING:
    227 		return AcpiGetHandle(NULL, elm->String.Pointer, handle);
    228 
    229 	default:
    230 		return AE_TYPE;
    231 	}
    232 }
    233 
    234 /*
    235  * Iterate over all objects in a package, and pass them all
    236  * to a function. If the called function returns non-AE_OK,
    237  * the iteration is stopped and that value is returned.
    238  */
    239 ACPI_STATUS
    240 acpi_foreach_package_object(ACPI_OBJECT *pkg,
    241     ACPI_STATUS (*func)(ACPI_OBJECT *, void *), void *arg)
    242 {
    243 	ACPI_STATUS rv = AE_OK;
    244 	uint32_t i;
    245 
    246 	if (pkg == NULL)
    247 		return AE_BAD_PARAMETER;
    248 
    249 	if (pkg->Type != ACPI_TYPE_PACKAGE)
    250 		return AE_TYPE;
    251 
    252 	for (i = 0; i < pkg->Package.Count; i++) {
    253 
    254 		rv = (*func)(&pkg->Package.Elements[i], arg);
    255 
    256 		if (ACPI_FAILURE(rv))
    257 			break;
    258 	}
    259 
    260 	return rv;
    261 }
    262 
    263 /*
    264  * Fetch data info the specified (empty) ACPI buffer.
    265  * Caller must free buf.Pointer by ACPI_FREE().
    266  */
    267 ACPI_STATUS
    268 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
    269     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
    270 {
    271 
    272 	buf->Pointer = NULL;
    273 	buf->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    274 
    275 	return (*getit)(handle, buf);
    276 }
    277 
    278 /*
    279  * Return a complete pathname from a handle.
    280  *
    281  * Note that the function uses static data storage;
    282  * if the data is needed for future use, it should be
    283  * copied before any subsequent calls overwrite it.
    284  */
    285 const char *
    286 acpi_name(ACPI_HANDLE handle)
    287 {
    288 	static char name[80];
    289 	ACPI_BUFFER buf;
    290 	ACPI_STATUS rv;
    291 
    292 	if (handle == NULL)
    293 		handle = ACPI_ROOT_OBJECT;
    294 
    295 	buf.Pointer = name;
    296 	buf.Length = sizeof(name);
    297 
    298 	rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
    299 
    300 	if (ACPI_FAILURE(rv))
    301 		return "UNKNOWN";
    302 
    303 	return name;
    304 }
    305 
    306 /*
    307  * Match given IDs against _HID and _CIDs.
    308  */
    309 int
    310 acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
    311 {
    312 	uint32_t i, n;
    313 	char *id;
    314 
    315 	while (*ids) {
    316 
    317 		if ((ad->Valid & ACPI_VALID_HID) != 0) {
    318 
    319 			if (pmatch(ad->HardwareId.String, *ids, NULL) == 2)
    320 				return 1;
    321 		}
    322 
    323 		if ((ad->Valid & ACPI_VALID_CID) != 0) {
    324 
    325 			n = ad->CompatibleIdList.Count;
    326 
    327 			for (i = 0; i < n; i++) {
    328 
    329 				id = ad->CompatibleIdList.Ids[i].String;
    330 
    331 				if (pmatch(id, *ids, NULL) == 2)
    332 					return 1;
    333 			}
    334 		}
    335 
    336 		ids++;
    337 	}
    338 
    339 	return 0;
    340 }
    341 
    342 /*
    343  * Match a PCI-defined bass-class, sub-class, and programming interface
    344  * against a handle's _CLS object.
    345  */
    346 int
    347 acpi_match_class(ACPI_HANDLE handle, uint8_t pci_class, uint8_t pci_subclass,
    348     uint8_t pci_interface)
    349 {
    350 	ACPI_BUFFER buf;
    351 	ACPI_OBJECT *obj;
    352 	ACPI_STATUS rv;
    353 	int match = 0;
    354 
    355 	rv = acpi_eval_struct(handle, "_CLS", &buf);
    356 	if (ACPI_FAILURE(rv))
    357 		goto done;
    358 
    359 	obj = buf.Pointer;
    360 	if (obj->Type != ACPI_TYPE_PACKAGE)
    361 		goto done;
    362 	if (obj->Package.Count != 3)
    363 		goto done;
    364 	if (obj->Package.Elements[0].Type != ACPI_TYPE_INTEGER ||
    365 	    obj->Package.Elements[1].Type != ACPI_TYPE_INTEGER ||
    366 	    obj->Package.Elements[2].Type != ACPI_TYPE_INTEGER)
    367 		goto done;
    368 
    369 	match = obj->Package.Elements[0].Integer.Value == pci_class &&
    370 		obj->Package.Elements[1].Integer.Value == pci_subclass &&
    371 		obj->Package.Elements[2].Integer.Value == pci_interface;
    372 
    373 done:
    374 	if (buf.Pointer)
    375 		ACPI_FREE(buf.Pointer);
    376 	return match;
    377 }
    378 
    379 /*
    380  * Match a device node from a handle.
    381  */
    382 struct acpi_devnode *
    383 acpi_match_node(ACPI_HANDLE handle)
    384 {
    385 	struct acpi_devnode *ad;
    386 	ACPI_STATUS rv;
    387 
    388 	if (handle == NULL)
    389 		return NULL;
    390 
    391 	rv = AcpiGetData(handle, acpi_clean_node, (void **)&ad);
    392 
    393 	if (ACPI_FAILURE(rv))
    394 		return NULL;
    395 
    396 	return ad;
    397 }
    398 
    399 /*
    400  * Permanently associate a device node with a handle.
    401  */
    402 void
    403 acpi_match_node_init(struct acpi_devnode *ad)
    404 {
    405 	(void)AcpiAttachData(ad->ad_handle, acpi_clean_node, ad);
    406 }
    407 
    408 static void
    409 acpi_clean_node(ACPI_HANDLE handle, void *aux)
    410 {
    411 	/* Nothing. */
    412 }
    413 
    414 /*
    415  * Match a handle from a cpu_info. Returns NULL on failure.
    416  *
    417  * Note that acpi_match_node() can be used if the device node
    418  * is also required.
    419  */
    420 ACPI_HANDLE
    421 acpi_match_cpu_info(struct cpu_info *ci)
    422 {
    423 	struct acpi_softc *sc = acpi_softc;
    424 	struct acpi_devnode *ad;
    425 	ACPI_INTEGER val;
    426 	ACPI_OBJECT *obj;
    427 	ACPI_BUFFER buf;
    428 	ACPI_HANDLE hdl;
    429 	ACPI_STATUS rv;
    430 
    431 	if (sc == NULL || acpi_active == 0)
    432 		return NULL;
    433 
    434 	/*
    435 	 * CPUs are declared in the ACPI namespace
    436 	 * either as a Processor() or as a Device().
    437 	 * In both cases the MADT entries are used
    438 	 * for the match (see ACPI 4.0, section 8.4).
    439 	 */
    440 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    441 
    442 		hdl = ad->ad_handle;
    443 
    444 		switch (ad->ad_type) {
    445 
    446 		case ACPI_TYPE_DEVICE:
    447 
    448 			if (acpi_match_hid(ad->ad_devinfo, acpicpu_ids) == 0)
    449 				break;
    450 
    451 			rv = acpi_eval_integer(hdl, "_UID", &val);
    452 
    453 			if (ACPI_SUCCESS(rv) && val == ci->ci_acpiid)
    454 				return hdl;
    455 
    456 			break;
    457 
    458 		case ACPI_TYPE_PROCESSOR:
    459 
    460 			rv = acpi_eval_struct(hdl, NULL, &buf);
    461 
    462 			if (ACPI_FAILURE(rv))
    463 				break;
    464 
    465 			obj = buf.Pointer;
    466 
    467 			if (obj->Processor.ProcId == ci->ci_acpiid) {
    468 				ACPI_FREE(buf.Pointer);
    469 				return hdl;
    470 			}
    471 
    472 			ACPI_FREE(buf.Pointer);
    473 			break;
    474 		}
    475 	}
    476 
    477 	return NULL;
    478 }
    479 
    480 /*
    481  * Match a CPU from a handle. Returns NULL on failure.
    482  */
    483 struct cpu_info *
    484 acpi_match_cpu_handle(ACPI_HANDLE hdl)
    485 {
    486 	struct cpu_info *ci;
    487 	ACPI_DEVICE_INFO *di;
    488 	CPU_INFO_ITERATOR cii;
    489 	ACPI_INTEGER val;
    490 	ACPI_OBJECT *obj;
    491 	ACPI_BUFFER buf;
    492 	ACPI_STATUS rv;
    493 
    494 	ci = NULL;
    495 	di = NULL;
    496 	buf.Pointer = NULL;
    497 
    498 	rv = AcpiGetObjectInfo(hdl, &di);
    499 
    500 	if (ACPI_FAILURE(rv))
    501 		return NULL;
    502 
    503 	switch (di->Type) {
    504 
    505 	case ACPI_TYPE_DEVICE:
    506 
    507 		if (acpi_match_hid(di, acpicpu_ids) == 0)
    508 			goto out;
    509 
    510 		rv = acpi_eval_integer(hdl, "_UID", &val);
    511 
    512 		if (ACPI_FAILURE(rv))
    513 			goto out;
    514 
    515 		break;
    516 
    517 	case ACPI_TYPE_PROCESSOR:
    518 
    519 		rv = acpi_eval_struct(hdl, NULL, &buf);
    520 
    521 		if (ACPI_FAILURE(rv))
    522 			goto out;
    523 
    524 		obj = buf.Pointer;
    525 		val = obj->Processor.ProcId;
    526 		break;
    527 
    528 	default:
    529 		goto out;
    530 	}
    531 
    532 	for (CPU_INFO_FOREACH(cii, ci)) {
    533 
    534 		if (ci->ci_acpiid == val)
    535 			goto out;
    536 	}
    537 
    538 	ci = NULL;
    539 
    540 out:
    541 	if (di != NULL)
    542 		ACPI_FREE(di);
    543 
    544 	if (buf.Pointer != NULL)
    545 		ACPI_FREE(buf.Pointer);
    546 
    547 	return ci;
    548 }
    549 
    550 struct acpi_irq_handler {
    551 	ACPI_HANDLE aih_hdl;
    552 	uint32_t aih_irq;
    553 	int (*aih_intr)(void *);
    554 };
    555 
    556 void *
    557 acpi_intr_establish(device_t dev, uint64_t c,
    558     unsigned int (*intr)(void *), void *iarg, const char *xname)
    559 {
    560 	ACPI_STATUS rv;
    561 	ACPI_HANDLE hdl = (void *)(uintptr_t)c;
    562 	struct acpi_resources res;
    563 	struct acpi_irq *irq;
    564 	struct acpi_irq_handler *aih = NULL;
    565 
    566 	rv = acpi_resource_parse(dev, hdl, "_CRS", &res,
    567 	    &acpi_resource_parse_ops_quiet);
    568 	if (ACPI_FAILURE(rv))
    569 		return NULL;
    570 
    571 	irq = acpi_res_irq(&res, 0);
    572 	if (irq == NULL)
    573 		goto end;
    574 
    575 	aih = kmem_alloc(sizeof(struct acpi_irq_handler), KM_NOSLEEP);
    576 	if (aih == NULL)
    577 		goto end;
    578 
    579 	aih->aih_hdl = hdl;
    580 	aih->aih_irq = irq->ar_irq;
    581 	rv = AcpiOsInstallInterruptHandler_xname(irq->ar_irq, intr, iarg, xname);
    582 	if (ACPI_FAILURE(rv)) {
    583 		kmem_free(aih, sizeof(struct acpi_irq_handler));
    584 		aih = NULL;
    585 	}
    586 end:
    587 	acpi_resource_cleanup(&res);
    588 	return aih;
    589 }
    590 
    591 void
    592 acpi_intr_disestablish(void *c, unsigned int (*intr)(void *))
    593 {
    594 	struct acpi_irq_handler *aih = c;
    595 
    596 	AcpiOsRemoveInterruptHandler(aih->aih_irq, intr);
    597 	kmem_free(aih, sizeof(struct acpi_irq_handler));
    598 	return;
    599 }
    600 
    601 const char *
    602 acpi_intr_string(void *c, char *buf, size_t size)
    603 {
    604 	struct acpi_irq_handler *aih = c;
    605 	intr_handle_t ih = aih->aih_irq;
    606 
    607 	return intr_string(ih, buf, size);
    608 }
    609