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 static void build_append_int_noprefix(GArray
*table
, uint64_t value
, int size
)
225 for (i
= 0; i
< size
; ++i
) {
226 build_append_byte(table
, value
& 0xFF);
231 void build_append_int(GArray
*table
, uint64_t value
)
234 build_append_byte(table
, 0x00); /* ZeroOp */
235 } else if (value
== 0x01) {
236 build_append_byte(table
, 0x01); /* OneOp */
237 } else if (value
<= 0xFF) {
238 build_append_byte(table
, 0x0A); /* BytePrefix */
239 build_append_int_noprefix(table
, value
, 1);
240 } else if (value
<= 0xFFFF) {
241 build_append_byte(table
, 0x0B); /* WordPrefix */
242 build_append_int_noprefix(table
, value
, 2);
243 } else if (value
<= 0xFFFFFFFF) {
244 build_append_byte(table
, 0x0C); /* DWordPrefix */
245 build_append_int_noprefix(table
, value
, 4);
247 build_append_byte(table
, 0x0E); /* QWordPrefix */
248 build_append_int_noprefix(table
, value
, 8);
252 static GPtrArray
*alloc_list
;
254 static Aml
*aml_alloc(void)
256 Aml
*var
= g_new0(typeof(*var
), 1);
258 g_ptr_array_add(alloc_list
, var
);
259 var
->block_flags
= AML_NO_OPCODE
;
260 var
->buf
= build_alloc_array();
264 static Aml
*aml_opcode(uint8_t op
)
266 Aml
*var
= aml_alloc();
269 var
->block_flags
= AML_OPCODE
;
273 static Aml
*aml_bundle(uint8_t op
, AmlBlockFlags flags
)
275 Aml
*var
= aml_alloc();
278 var
->block_flags
= flags
;
282 static void aml_free(gpointer data
)
285 build_free_array(var
->buf
);
288 Aml
*init_aml_allocator(void)
293 alloc_list
= g_ptr_array_new_with_free_func(aml_free
);
298 void free_aml_allocator(void)
300 g_ptr_array_free(alloc_list
, true);
304 /* pack data with DefBuffer encoding */
305 static void build_buffer(GArray
*array
, uint8_t op
)
307 GArray
*data
= build_alloc_array();
309 build_append_int(data
, array
->len
);
310 g_array_prepend_vals(array
, data
->data
, data
->len
);
311 build_free_array(data
);
312 build_package(array
, op
);
315 void aml_append(Aml
*parent_ctx
, Aml
*child
)
317 switch (child
->block_flags
) {
319 build_append_byte(parent_ctx
->buf
, child
->op
);
321 case AML_EXT_PACKAGE
:
322 build_extop_package(child
->buf
, child
->op
);
325 build_package(child
->buf
, child
->op
);
327 case AML_RES_TEMPLATE
:
328 build_append_byte(child
->buf
, 0x79); /* EndTag */
330 * checksum operations are treated as succeeded if checksum
331 * field is zero. [ACPI Spec 1.0b, 6.4.2.8 End Tag]
333 build_append_byte(child
->buf
, 0);
334 /* fall through, to pack resources in buffer */
336 build_buffer(child
->buf
, child
->op
);
344 build_append_array(parent_ctx
->buf
, child
->buf
);
347 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefScope */
348 Aml
*aml_scope(const char *name_format
, ...)
351 Aml
*var
= aml_bundle(0x10 /* ScopeOp */, AML_PACKAGE
);
352 va_start(ap
, name_format
);
353 build_append_namestringv(var
->buf
, name_format
, ap
);
358 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefReturn */
359 Aml
*aml_return(Aml
*val
)
361 Aml
*var
= aml_opcode(0xA4 /* ReturnOp */);
362 aml_append(var
, val
);
367 * ACPI 1.0b: 16.2.3 Data Objects Encoding:
368 * encodes: ByteConst, WordConst, DWordConst, QWordConst, ZeroOp, OneOp
370 Aml
*aml_int(const uint64_t val
)
372 Aml
*var
= aml_alloc();
373 build_append_int(var
->buf
, val
);
378 * helper to construct NameString, which returns Aml object
379 * for using with aml_append or other aml_* terms
381 Aml
*aml_name(const char *name_format
, ...)
384 Aml
*var
= aml_alloc();
385 va_start(ap
, name_format
);
386 build_append_namestringv(var
->buf
, name_format
, ap
);
391 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefName */
392 Aml
*aml_name_decl(const char *name
, Aml
*val
)
394 Aml
*var
= aml_opcode(0x08 /* NameOp */);
395 build_append_namestring(var
->buf
, "%s", name
);
396 aml_append(var
, val
);
400 /* ACPI 1.0b: 16.2.6.1 Arg Objects Encoding */
401 Aml
*aml_arg(int pos
)
404 uint8_t op
= 0x68 /* ARG0 op */ + pos
;
407 var
= aml_opcode(op
);
411 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefStore */
412 Aml
*aml_store(Aml
*val
, Aml
*target
)
414 Aml
*var
= aml_opcode(0x70 /* StoreOp */);
415 aml_append(var
, val
);
416 aml_append(var
, target
);
420 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAnd */
421 Aml
*aml_and(Aml
*arg1
, Aml
*arg2
)
423 Aml
*var
= aml_opcode(0x7B /* AndOp */);
424 aml_append(var
, arg1
);
425 aml_append(var
, arg2
);
426 build_append_int(var
->buf
, 0x00 /* NullNameOp */);
430 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */
431 Aml
*aml_if(Aml
*predicate
)
433 Aml
*var
= aml_bundle(0xA0 /* IfOp */, AML_PACKAGE
);
434 aml_append(var
, predicate
);
438 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */
439 Aml
*aml_method(const char *name
, int arg_count
)
441 Aml
*var
= aml_bundle(0x14 /* MethodOp */, AML_PACKAGE
);
442 build_append_namestring(var
->buf
, "%s", name
);
443 build_append_byte(var
->buf
, arg_count
); /* MethodFlags: ArgCount */
447 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */
448 Aml
*aml_device(const char *name_format
, ...)
451 Aml
*var
= aml_bundle(0x82 /* DeviceOp */, AML_EXT_PACKAGE
);
452 va_start(ap
, name_format
);
453 build_append_namestringv(var
->buf
, name_format
, ap
);