xref: /freebsd/sys/dev/acpica/acpi_package.c (revision c697fb7f)
1 /*-
2  * Copyright (c) 2003 Nate Lawson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/bus.h>
33 #include <sys/sbuf.h>
34 
35 #include <machine/bus.h>
36 #include <machine/resource.h>
37 #include <sys/rman.h>
38 
39 #include <contrib/dev/acpica/include/acpi.h>
40 
41 #include <dev/acpica/acpivar.h>
42 
43 /*
44  * Package manipulation convenience functions
45  */
46 
47 int
48 acpi_PkgInt(ACPI_OBJECT *res, int idx, UINT64 *dst)
49 {
50     ACPI_OBJECT		*obj;
51 
52     obj = &res->Package.Elements[idx];
53     if (obj->Type != ACPI_TYPE_INTEGER)
54 	return (EINVAL);
55     *dst = obj->Integer.Value;
56 
57     return (0);
58 }
59 
60 int
61 acpi_PkgInt32(ACPI_OBJECT *res, int idx, uint32_t *dst)
62 {
63     UINT64		tmp;
64     int			error;
65 
66     error = acpi_PkgInt(res, idx, &tmp);
67     if (error == 0)
68 	*dst = (uint32_t)tmp;
69 
70     return (error);
71 }
72 
73 int
74 acpi_PkgInt16(ACPI_OBJECT *res, int idx, uint16_t *dst)
75 {
76     UINT64		tmp;
77     int			error;
78 
79     error = acpi_PkgInt(res, idx, &tmp);
80     if (error == 0)
81 	*dst = (uint16_t)tmp;
82 
83     return (error);
84 }
85 
86 int
87 acpi_PkgStr(ACPI_OBJECT *res, int idx, void *dst, size_t size)
88 {
89     ACPI_OBJECT		*obj;
90     void		*ptr;
91     size_t		 length;
92 
93     obj = &res->Package.Elements[idx];
94     if (obj == NULL)
95 	return (EINVAL);
96 
97     switch (obj->Type) {
98     case ACPI_TYPE_STRING:
99 	ptr = obj->String.Pointer;
100 	length = obj->String.Length;
101 	break;
102     case ACPI_TYPE_BUFFER:
103 	ptr = obj->Buffer.Pointer;
104 	length = obj->Buffer.Length;
105 	break;
106     default:
107 	return (EINVAL);
108     }
109 
110     /* Make sure string will fit, including terminating NUL */
111     if (++length > size)
112 	return (E2BIG);
113 
114     strlcpy(dst, ptr, length);
115     return (0);
116 }
117 
118 int
119 acpi_PkgGas(device_t dev, ACPI_OBJECT *res, int idx, int *type, int *rid,
120     struct resource **dst, u_int flags)
121 {
122     ACPI_GENERIC_ADDRESS gas;
123     ACPI_OBJECT *obj;
124 
125     obj = &res->Package.Elements[idx];
126     if (obj == NULL || obj->Type != ACPI_TYPE_BUFFER ||
127 	obj->Buffer.Length < sizeof(ACPI_GENERIC_ADDRESS) + 3)
128 	return (EINVAL);
129 
130     memcpy(&gas, obj->Buffer.Pointer + 3, sizeof(gas));
131 
132     return (acpi_bus_alloc_gas(dev, type, rid, &gas, dst, flags));
133 }
134 
135 int
136 acpi_PkgFFH_IntelCpu(ACPI_OBJECT *res, int idx, int *vendor, int *class,
137     uint64_t *address, int *accsize)
138 {
139     ACPI_GENERIC_ADDRESS gas;
140     ACPI_OBJECT *obj;
141 
142     obj = &res->Package.Elements[idx];
143     if (obj == NULL || obj->Type != ACPI_TYPE_BUFFER ||
144 	obj->Buffer.Length < sizeof(ACPI_GENERIC_ADDRESS) + 3)
145 	return (EINVAL);
146 
147     memcpy(&gas, obj->Buffer.Pointer + 3, sizeof(gas));
148     if (gas.SpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE)
149 	return (ERESTART);
150     *vendor = gas.BitWidth;
151     *class = gas.BitOffset;
152     *address = gas.Address;
153     *accsize = gas.AccessWidth;
154     return (0);
155 }
156 
157 ACPI_HANDLE
158 acpi_GetReference(ACPI_HANDLE scope, ACPI_OBJECT *obj)
159 {
160     ACPI_HANDLE h;
161 
162     if (obj == NULL)
163 	return (NULL);
164 
165     switch (obj->Type) {
166     case ACPI_TYPE_LOCAL_REFERENCE:
167     case ACPI_TYPE_ANY:
168 	h = obj->Reference.Handle;
169 	break;
170     case ACPI_TYPE_STRING:
171 	/*
172 	 * The String object usually contains a fully-qualified path, so
173 	 * scope can be NULL.
174 	 *
175 	 * XXX This may not always be the case.
176 	 */
177 	if (ACPI_FAILURE(AcpiGetHandle(scope, obj->String.Pointer, &h)))
178 	    h = NULL;
179 	break;
180     default:
181 	h = NULL;
182 	break;
183     }
184 
185     return (h);
186 }
187