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