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