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/param.h>
28 #include <sys/kernel.h>
29 #include <sys/bus.h>
30 #include <sys/sbuf.h>
31
32 #include <machine/bus.h>
33 #include <machine/resource.h>
34 #include <sys/rman.h>
35
36 #include <contrib/dev/acpica/include/acpi.h>
37
38 #include <dev/acpica/acpivar.h>
39
40 /*
41 * Package manipulation convenience functions
42 */
43
44 int
acpi_PkgInt(ACPI_OBJECT * res,int idx,UINT64 * dst)45 acpi_PkgInt(ACPI_OBJECT *res, int idx, UINT64 *dst)
46 {
47 ACPI_OBJECT *obj;
48
49 obj = &res->Package.Elements[idx];
50 if (obj->Type != ACPI_TYPE_INTEGER)
51 return (EINVAL);
52 *dst = obj->Integer.Value;
53
54 return (0);
55 }
56
57 int
acpi_PkgInt32(ACPI_OBJECT * res,int idx,uint32_t * dst)58 acpi_PkgInt32(ACPI_OBJECT *res, int idx, uint32_t *dst)
59 {
60 UINT64 tmp;
61 int error;
62
63 error = acpi_PkgInt(res, idx, &tmp);
64 if (error == 0)
65 *dst = (uint32_t)tmp;
66
67 return (error);
68 }
69
70 int
acpi_PkgInt16(ACPI_OBJECT * res,int idx,uint16_t * dst)71 acpi_PkgInt16(ACPI_OBJECT *res, int idx, uint16_t *dst)
72 {
73 UINT64 tmp;
74 int error;
75
76 error = acpi_PkgInt(res, idx, &tmp);
77 if (error == 0)
78 *dst = (uint16_t)tmp;
79
80 return (error);
81 }
82
83 int
acpi_PkgStr(ACPI_OBJECT * res,int idx,void * dst,size_t size)84 acpi_PkgStr(ACPI_OBJECT *res, int idx, void *dst, size_t size)
85 {
86 ACPI_OBJECT *obj;
87 void *ptr;
88 size_t length;
89
90 obj = &res->Package.Elements[idx];
91 if (obj == NULL)
92 return (EINVAL);
93
94 switch (obj->Type) {
95 case ACPI_TYPE_STRING:
96 ptr = obj->String.Pointer;
97 length = obj->String.Length;
98 break;
99 case ACPI_TYPE_BUFFER:
100 ptr = obj->Buffer.Pointer;
101 length = obj->Buffer.Length;
102 break;
103 default:
104 return (EINVAL);
105 }
106
107 /* Make sure string will fit, including terminating NUL */
108 if (++length > size)
109 return (E2BIG);
110
111 strlcpy(dst, ptr, length);
112 return (0);
113 }
114
115 int
acpi_PkgGas(device_t dev,ACPI_OBJECT * res,int idx,int * type,int * rid,struct resource ** dst,u_int flags)116 acpi_PkgGas(device_t dev, ACPI_OBJECT *res, int idx, int *type, int *rid,
117 struct resource **dst, u_int flags)
118 {
119 ACPI_GENERIC_ADDRESS gas;
120 ACPI_OBJECT *obj;
121
122 obj = &res->Package.Elements[idx];
123 if (obj == NULL || obj->Type != ACPI_TYPE_BUFFER ||
124 obj->Buffer.Length < sizeof(ACPI_GENERIC_ADDRESS) + 3)
125 return (EINVAL);
126
127 memcpy(&gas, obj->Buffer.Pointer + 3, sizeof(gas));
128
129 return (acpi_bus_alloc_gas(dev, type, rid, &gas, dst, flags));
130 }
131
132 int
acpi_PkgFFH_IntelCpu(ACPI_OBJECT * res,int idx,int * vendor,int * class,uint64_t * address,int * accsize)133 acpi_PkgFFH_IntelCpu(ACPI_OBJECT *res, int idx, int *vendor, int *class,
134 uint64_t *address, int *accsize)
135 {
136 ACPI_GENERIC_ADDRESS gas;
137 ACPI_OBJECT *obj;
138
139 obj = &res->Package.Elements[idx];
140 if (obj == NULL || obj->Type != ACPI_TYPE_BUFFER ||
141 obj->Buffer.Length < sizeof(ACPI_GENERIC_ADDRESS) + 3)
142 return (EINVAL);
143
144 memcpy(&gas, obj->Buffer.Pointer + 3, sizeof(gas));
145 if (gas.SpaceId != ACPI_ADR_SPACE_FIXED_HARDWARE)
146 return (ERESTART);
147 *vendor = gas.BitWidth;
148 *class = gas.BitOffset;
149 *address = gas.Address;
150 *accsize = gas.AccessWidth;
151 return (0);
152 }
153
154 ACPI_HANDLE
acpi_GetReference(ACPI_HANDLE scope,ACPI_OBJECT * obj)155 acpi_GetReference(ACPI_HANDLE scope, ACPI_OBJECT *obj)
156 {
157 ACPI_HANDLE h;
158
159 if (obj == NULL)
160 return (NULL);
161
162 switch (obj->Type) {
163 case ACPI_TYPE_LOCAL_REFERENCE:
164 case ACPI_TYPE_ANY:
165 h = obj->Reference.Handle;
166 break;
167 case ACPI_TYPE_STRING:
168 /*
169 * The String object usually contains a fully-qualified path, so
170 * scope can be NULL.
171 *
172 * XXX This may not always be the case.
173 */
174 if (ACPI_FAILURE(AcpiGetHandle(scope, obj->String.Pointer, &h)))
175 h = NULL;
176 break;
177 default:
178 h = NULL;
179 break;
180 }
181
182 return (h);
183 }
184