acpi: add aml_if() term
[qemu/ar7.git] / hw / acpi / aml-build.c
blobd19d1fb74aeaaa58068fac760a4c18305338654b
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/>.
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
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
57 static void
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 */
61 int len;
63 len = strlen(seg);
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);
71 static void
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 */
75 char *s;
76 int len;
77 va_list va_len;
78 char **segs;
79 char **segs_iter;
80 int seg_count = 0;
82 va_copy(va_len, ap);
83 len = vsnprintf(NULL, 0, format, va_len);
84 va_end(va_len);
85 len += 1;
86 s = g_new(typeof(*s), len);
88 len = vsnprintf(s, len, format, ap);
90 segs = g_strsplit(s, ".", 0);
91 g_free(s);
93 /* count segments */
94 segs_iter = segs;
95 while (*segs_iter) {
96 ++segs_iter;
97 ++seg_count;
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 */
106 s = *segs;
107 while (*s == '\\' || *s == '^') {
108 build_append_byte(array, *s);
109 ++s;
112 switch (seg_count) {
113 case 1:
114 if (!*s) {
115 build_append_byte(array, 0x0); /* NullName */
116 } else {
117 build_append_nameseg(array, s);
119 break;
121 case 2:
122 build_append_byte(array, 0x2E); /* DualNamePrefix */
123 build_append_nameseg(array, s);
124 build_append_nameseg(array, segs[1]);
125 break;
126 default:
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;
135 while (*segs_iter) {
136 build_append_nameseg(array, *segs_iter);
137 ++segs_iter;
139 break;
141 g_strfreev(segs);
144 void build_append_namestring(GArray *array, const char *format, ...)
146 va_list ap;
148 va_start(ap, format);
149 build_append_namestringv(array, format, ap);
150 va_end(ap);
153 /* 5.4 Definition Block Encoding */
154 enum {
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)
163 uint8_t byte;
164 unsigned length = package->len;
165 unsigned length_bytes;
167 if (length + 1 < (1 << PACKAGE_LENGTH_1BYTE_SHIFT)) {
168 length_bytes = 1;
169 } else if (length + 2 < (1 << PACKAGE_LENGTH_3BYTE_SHIFT)) {
170 length_bytes = 2;
171 } else if (length + 3 < (1 << PACKAGE_LENGTH_4BYTE_SHIFT)) {
172 length_bytes = 3;
173 } else {
174 length_bytes = 4;
177 /* PkgLength is the length of the inclusive length of the data. */
178 length += length_bytes;
180 switch (length_bytes) {
181 case 1:
182 byte = length;
183 build_prepend_byte(package, byte);
184 return;
185 case 4:
186 byte = length >> PACKAGE_LENGTH_4BYTE_SHIFT;
187 build_prepend_byte(package, byte);
188 length &= (1 << PACKAGE_LENGTH_4BYTE_SHIFT) - 1;
189 /* fall through */
190 case 3:
191 byte = length >> PACKAGE_LENGTH_3BYTE_SHIFT;
192 build_prepend_byte(package, byte);
193 length &= (1 << PACKAGE_LENGTH_3BYTE_SHIFT) - 1;
194 /* fall through */
195 case 2:
196 byte = length >> PACKAGE_LENGTH_2BYTE_SHIFT;
197 build_prepend_byte(package, byte);
198 length &= (1 << PACKAGE_LENGTH_2BYTE_SHIFT) - 1;
199 /* fall through */
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)
223 uint8_t prefix;
224 int i;
226 switch (size) {
227 case 1:
228 prefix = 0x0A; /* BytePrefix */
229 break;
230 case 2:
231 prefix = 0x0B; /* WordPrefix */
232 break;
233 case 4:
234 prefix = 0x0C; /* DWordPrefix */
235 break;
236 default:
237 assert(0);
238 return;
240 build_append_byte(table, prefix);
241 for (i = 0; i < size; ++i) {
242 build_append_byte(table, value & 0xFF);
243 value = value >> 8;
247 void build_append_int(GArray *table, uint32_t value)
249 if (value == 0x00) {
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);
257 } else {
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();
271 return var;
274 static Aml *aml_bundle(uint8_t op, AmlBlockFlags flags)
276 Aml *var = aml_alloc();
278 var->op = op;
279 var->block_flags = flags;
280 return var;
283 static void aml_free(gpointer data)
285 Aml *var = data;
286 build_free_array(var->buf);
289 Aml *init_aml_allocator(void)
291 Aml *var;
293 assert(!alloc_list);
294 alloc_list = g_ptr_array_new_with_free_func(aml_free);
295 var = aml_alloc();
296 return var;
299 void free_aml_allocator(void)
301 g_ptr_array_free(alloc_list, true);
302 alloc_list = 0;
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) {
319 case AML_OPCODE:
320 build_append_byte(parent_ctx->buf, child->op);
321 break;
322 case AML_EXT_PACKAGE:
323 build_extop_package(child->buf, child->op);
324 break;
325 case AML_PACKAGE:
326 build_package(child->buf, child->op);
327 break;
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 */
336 case AML_BUFFER:
337 build_buffer(child->buf, child->op);
338 break;
339 case AML_NO_OPCODE:
340 break;
341 default:
342 assert(0);
343 break;
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, ...)
351 va_list ap;
352 Aml *var = aml_bundle(0x10 /* ScopeOp */, AML_PACKAGE);
353 va_start(ap, name_format);
354 build_append_namestringv(var->buf, name_format, ap);
355 va_end(ap);
356 return var;
359 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */
360 Aml *aml_if(Aml *predicate)
362 Aml *var = aml_bundle(0xA0 /* IfOp */, AML_PACKAGE);
363 aml_append(var, predicate);
364 return var;
367 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */
368 Aml *aml_method(const char *name, int arg_count)
370 Aml *var = aml_bundle(0x14 /* MethodOp */, AML_PACKAGE);
371 build_append_namestring(var->buf, "%s", name);
372 build_append_byte(var->buf, arg_count); /* MethodFlags: ArgCount */
373 return var;
376 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */
377 Aml *aml_device(const char *name_format, ...)
379 va_list ap;
380 Aml *var = aml_bundle(0x82 /* DeviceOp */, AML_EXT_PACKAGE);
381 va_start(ap, name_format);
382 build_append_namestringv(var->buf, name_format, ap);
383 va_end(ap);
384 return var;