xref: /netbsd-src/sys/external/bsd/drm2/linux/linux_acpi.c (revision c981a8797da8d48f7f3365847615f171a4d7091e)
1 /*	$NetBSD: linux_acpi.c,v 1.1 2022/02/27 14:22:21 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2022 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: linux_acpi.c,v 1.1 2022/02/27 14:22:21 riastradh Exp $");
31 
32 #include <linux/acpi.h>
33 
34 union acpi_object *
35 acpi_evaluate_dsm(acpi_handle handle, const guid_t *uuid, u64 rev, u64 func,
36     union acpi_object *argv4)
37 {
38 	ACPI_OBJECT_LIST arg;
39 	ACPI_OBJECT params[4];
40 	ACPI_BUFFER buf;
41 	ACPI_STATUS rv;
42 
43 	if (handle == NULL)
44 		handle = ACPI_ROOT_OBJECT;
45 
46 	arg.Count = 4;
47 	arg.Pointer = params;
48 	params[0].Type = ACPI_TYPE_BUFFER;
49 	params[0].Buffer.Length = 16;
50 	params[0].Buffer.Pointer = (char *)__UNCONST(uuid);
51 	params[1].Type = ACPI_TYPE_INTEGER;
52 	params[1].Integer.Value = rev;
53 	params[2].Type = ACPI_TYPE_INTEGER;
54 	params[2].Integer.Value = func;
55 	if (argv4 != NULL) {
56 		params[3] = *argv4;
57 	} else {
58 		params[3].Type = ACPI_TYPE_PACKAGE;
59 		params[3].Package.Count = 0;
60 		params[3].Package.Elements = NULL;
61 	}
62 
63 	buf.Pointer = NULL;
64 	buf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
65 
66 	rv = AcpiEvaluateObject(handle, "_DSM", &arg, &buf);
67 	if (ACPI_SUCCESS(rv))
68 		return (ACPI_OBJECT *)buf.Pointer;
69 	return NULL;
70 }
71 
72 union acpi_object *
73 acpi_evaluate_dsm_typed(acpi_handle handle, const guid_t *uuid, u64 rev,
74     u64 func, union acpi_object *argv4, acpi_object_type type)
75 {
76 	union acpi_object *obj;
77 
78 	obj = acpi_evaluate_dsm(handle, uuid, rev, func, argv4);
79 	if (obj != NULL && obj->Type != type) {
80 		ACPI_FREE(obj);
81 		obj = NULL;
82 	}
83 	return obj;
84 }
85 
86 #define	ACPI_INIT_DSM_ARGV4(cnt, eles)		\
87 {						\
88 	.Package.Type = ACPI_TYPE_PACKAGE,	\
89 	.Package.Count = (cnt),			\
90 	.Package.Elements = (eles)		\
91 }
92 
93 bool
94 acpi_check_dsm(acpi_handle handle, const guid_t *uuid, u64 rev, u64 funcs)
95 {
96 	ACPI_OBJECT *obj;
97 	uint64_t mask = 0;
98 	int i;
99 
100 	if (funcs == 0)
101 		return false;
102 
103 	obj = acpi_evaluate_dsm(handle, uuid, rev, 0, NULL);
104 	if (obj == NULL)
105 		return false;
106 
107 	if (obj->Type == ACPI_TYPE_INTEGER)
108 		mask = obj->Integer.Value;
109 	else if (obj->Type == ACPI_TYPE_BUFFER)
110 		for (i = 0; i < obj->Buffer.Length && i < 8; i++)
111 			mask |= (uint64_t)obj->Buffer.Pointer[i] << (i * 8);
112 	ACPI_FREE(obj);
113 
114 	if ((mask & 0x1) == 0x1 && (mask & funcs) == funcs)
115 		return true;
116 	return false;
117 }
118