1 /* Support for generating ACPI tables and passing them to Guests
3 * Copyright (C) 2015 Red Hat Inc
5 * Author: Michael S. Tsirkin <mst@redhat.com>
6 * Author: Igor Mammedov <imammedo@redhat.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
27 #include "hw/acpi/aml-build.h"
28 #include "qemu/bswap.h"
30 GArray
*build_alloc_array(void)
32 return g_array_new(false, true /* clear */, 1);
35 void build_free_array(GArray
*array
)
37 g_array_free(array
, true);
40 void build_prepend_byte(GArray
*array
, uint8_t val
)
42 g_array_prepend_val(array
, val
);
45 void build_append_byte(GArray
*array
, uint8_t val
)
47 g_array_append_val(array
, val
);
50 void build_append_array(GArray
*array
, GArray
*val
)
52 g_array_append_vals(array
, val
->data
, val
->len
);
55 #define ACPI_NAMESEG_LEN 4
58 build_append_nameseg(GArray
*array
, const char *seg
)
60 /* It would be nicer to use g_string_vprintf but it's only there in 2.22 */
64 assert(len
<= ACPI_NAMESEG_LEN
);
66 g_array_append_vals(array
, seg
, len
);
67 /* Pad up to ACPI_NAMESEG_LEN characters if necessary. */
68 g_array_append_vals(array
, "____", ACPI_NAMESEG_LEN
- len
);
72 build_append_namestringv(GArray
*array
, const char *format
, va_list ap
)
74 /* It would be nicer to use g_string_vprintf but it's only there in 2.22 */
83 len
= vsnprintf(NULL
, 0, format
, va_len
);
86 s
= g_new(typeof(*s
), len
);
88 len
= vsnprintf(s
, len
, format
, ap
);
90 segs
= g_strsplit(s
, ".", 0);
100 * ACPI 5.0 spec: 20.2.2 Name Objects Encoding:
101 * "SegCount can be from 1 to 255"
103 assert(seg_count
> 0 && seg_count
<= 255);
105 /* handle RootPath || PrefixPath */
107 while (*s
== '\\' || *s
== '^') {
108 build_append_byte(array
, *s
);
115 build_append_byte(array
, 0x0); /* NullName */
117 build_append_nameseg(array
, s
);
122 build_append_byte(array
, 0x2E); /* DualNamePrefix */
123 build_append_nameseg(array
, s
);
124 build_append_nameseg(array
, segs
[1]);
127 build_append_byte(array
, 0x2F); /* MultiNamePrefix */
128 build_append_byte(array
, seg_count
);
130 /* handle the 1st segment manually due to prefix/root path */
131 build_append_nameseg(array
, s
);
133 /* add the rest of segments */
134 segs_iter
= segs
+ 1;
136 build_append_nameseg(array
, *segs_iter
);
144 void build_append_namestring(GArray
*array
, const char *format
, ...)
148 va_start(ap
, format
);
149 build_append_namestringv(array
, format
, ap
);
153 /* 5.4 Definition Block Encoding */
155 PACKAGE_LENGTH_1BYTE_SHIFT
= 6, /* Up to 63 - use extra 2 bits. */
156 PACKAGE_LENGTH_2BYTE_SHIFT
= 4,
157 PACKAGE_LENGTH_3BYTE_SHIFT
= 12,
158 PACKAGE_LENGTH_4BYTE_SHIFT
= 20,
161 void build_prepend_package_length(GArray
*package
)
164 unsigned length
= package
->len
;
165 unsigned length_bytes
;
167 if (length
+ 1 < (1 << PACKAGE_LENGTH_1BYTE_SHIFT
)) {
169 } else if (length
+ 2 < (1 << PACKAGE_LENGTH_3BYTE_SHIFT
)) {
171 } else if (length
+ 3 < (1 << PACKAGE_LENGTH_4BYTE_SHIFT
)) {
177 /* PkgLength is the length of the inclusive length of the data. */
178 length
+= length_bytes
;
180 switch (length_bytes
) {
183 build_prepend_byte(package
, byte
);
186 byte
= length
>> PACKAGE_LENGTH_4BYTE_SHIFT
;
187 build_prepend_byte(package
, byte
);
188 length
&= (1 << PACKAGE_LENGTH_4BYTE_SHIFT
) - 1;
191 byte
= length
>> PACKAGE_LENGTH_3BYTE_SHIFT
;
192 build_prepend_byte(package
, byte
);
193 length
&= (1 << PACKAGE_LENGTH_3BYTE_SHIFT
) - 1;
196 byte
= length
>> PACKAGE_LENGTH_2BYTE_SHIFT
;
197 build_prepend_byte(package
, byte
);
198 length
&= (1 << PACKAGE_LENGTH_2BYTE_SHIFT
) - 1;
202 * Most significant two bits of byte zero indicate how many following bytes
203 * are in PkgLength encoding.
205 byte
= ((length_bytes
- 1) << PACKAGE_LENGTH_1BYTE_SHIFT
) | length
;
206 build_prepend_byte(package
, byte
);
209 void build_package(GArray
*package
, uint8_t op
)
211 build_prepend_package_length(package
);
212 build_prepend_byte(package
, op
);
215 void build_extop_package(GArray
*package
, uint8_t op
)
217 build_package(package
, op
);
218 build_prepend_byte(package
, 0x5B); /* ExtOpPrefix */
221 void build_append_value(GArray
*table
, uint32_t value
, int size
)
228 prefix
= 0x0A; /* BytePrefix */
231 prefix
= 0x0B; /* WordPrefix */
234 prefix
= 0x0C; /* DWordPrefix */
240 build_append_byte(table
, prefix
);
241 for (i
= 0; i
< size
; ++i
) {
242 build_append_byte(table
, value
& 0xFF);
247 void build_append_int(GArray
*table
, uint32_t value
)
250 build_append_byte(table
, 0x00); /* ZeroOp */
251 } else if (value
== 0x01) {
252 build_append_byte(table
, 0x01); /* OneOp */
253 } else if (value
<= 0xFF) {
254 build_append_value(table
, value
, 1);
255 } else if (value
<= 0xFFFF) {
256 build_append_value(table
, value
, 2);
258 build_append_value(table
, value
, 4);
262 static GPtrArray
*alloc_list
;
264 static Aml
*aml_alloc(void)
266 Aml
*var
= g_new0(typeof(*var
), 1);
268 g_ptr_array_add(alloc_list
, var
);
269 var
->block_flags
= AML_NO_OPCODE
;
270 var
->buf
= build_alloc_array();
274 static Aml
*aml_bundle(uint8_t op
, AmlBlockFlags flags
)
276 Aml
*var
= aml_alloc();
279 var
->block_flags
= flags
;
283 static void aml_free(gpointer data
)
286 build_free_array(var
->buf
);
289 Aml
*init_aml_allocator(void)
294 alloc_list
= g_ptr_array_new_with_free_func(aml_free
);
299 void free_aml_allocator(void)
301 g_ptr_array_free(alloc_list
, true);
305 /* pack data with DefBuffer encoding */
306 static void build_buffer(GArray
*array
, uint8_t op
)
308 GArray
*data
= build_alloc_array();
310 build_append_int(data
, array
->len
);
311 g_array_prepend_vals(array
, data
->data
, data
->len
);
312 build_free_array(data
);
313 build_package(array
, op
);
316 void aml_append(Aml
*parent_ctx
, Aml
*child
)
318 switch (child
->block_flags
) {
320 build_append_byte(parent_ctx
->buf
, child
->op
);
322 case AML_EXT_PACKAGE
:
323 build_extop_package(child
->buf
, child
->op
);
326 build_package(child
->buf
, child
->op
);
328 case AML_RES_TEMPLATE
:
329 build_append_byte(child
->buf
, 0x79); /* EndTag */
331 * checksum operations are treated as succeeded if checksum
332 * field is zero. [ACPI Spec 1.0b, 6.4.2.8 End Tag]
334 build_append_byte(child
->buf
, 0);
335 /* fall through, to pack resources in buffer */
337 build_buffer(child
->buf
, child
->op
);
345 build_append_array(parent_ctx
->buf
, child
->buf
);
348 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefScope */
349 Aml
*aml_scope(const char *name_format
, ...)
352 Aml
*var
= aml_bundle(0x10 /* ScopeOp */, AML_PACKAGE
);
353 va_start(ap
, name_format
);
354 build_append_namestringv(var
->buf
, name_format
, ap
);
359 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */
360 Aml
*aml_method(const char *name
, int arg_count
)
362 Aml
*var
= aml_bundle(0x14 /* MethodOp */, AML_PACKAGE
);
363 build_append_namestring(var
->buf
, "%s", name
);
364 build_append_byte(var
->buf
, arg_count
); /* MethodFlags: ArgCount */
368 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */
369 Aml
*aml_device(const char *name_format
, ...)
372 Aml
*var
= aml_bundle(0x82 /* DeviceOp */, AML_EXT_PACKAGE
);
373 va_start(ap
, name_format
);
374 build_append_namestringv(var
->buf
, name_format
, ap
);