]> Git Repo - qemu.git/blame - hw/acpi/aml-build.c
acpi: add aml_notify() term
[qemu.git] / hw / acpi / aml-build.c
CommitLineData
19934e0e
IM
1/* Support for generating ACPI tables and passing them to Guests
2 *
3 * Copyright (C) 2015 Red Hat Inc
4 *
5 * Author: Michael S. Tsirkin <[email protected]>
6 * Author: Igor Mammedov <[email protected]>
7 *
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.
12
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.
17
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/>.
20 */
21
22#include <stdio.h>
23#include <stdarg.h>
24#include <assert.h>
25#include <stdbool.h>
eae8bded 26#include <string.h>
19934e0e 27#include "hw/acpi/aml-build.h"
0f2707e4 28#include "qemu/bswap.h"
19934e0e
IM
29
30GArray *build_alloc_array(void)
31{
32 return g_array_new(false, true /* clear */, 1);
33}
34
35void build_free_array(GArray *array)
36{
37 g_array_free(array, true);
38}
39
40void build_prepend_byte(GArray *array, uint8_t val)
41{
42 g_array_prepend_val(array, val);
43}
44
45void build_append_byte(GArray *array, uint8_t val)
46{
47 g_array_append_val(array, val);
48}
49
50void build_append_array(GArray *array, GArray *val)
51{
52 g_array_append_vals(array, val->data, val->len);
53}
54
55#define ACPI_NAMESEG_LEN 4
56
eae8bded
IM
57static void
58build_append_nameseg(GArray *array, const char *seg)
19934e0e
IM
59{
60 /* It would be nicer to use g_string_vprintf but it's only there in 2.22 */
19934e0e 61 int len;
19934e0e 62
eae8bded 63 len = strlen(seg);
19934e0e
IM
64 assert(len <= ACPI_NAMESEG_LEN);
65
eae8bded 66 g_array_append_vals(array, seg, len);
19934e0e
IM
67 /* Pad up to ACPI_NAMESEG_LEN characters if necessary. */
68 g_array_append_vals(array, "____", ACPI_NAMESEG_LEN - len);
69}
70
eae8bded
IM
71static void
72build_append_namestringv(GArray *array, const char *format, va_list ap)
73{
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;
81
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);
87
88 len = vsnprintf(s, len, format, ap);
89
90 segs = g_strsplit(s, ".", 0);
91 g_free(s);
92
93 /* count segments */
94 segs_iter = segs;
95 while (*segs_iter) {
96 ++segs_iter;
97 ++seg_count;
98 }
99 /*
100 * ACPI 5.0 spec: 20.2.2 Name Objects Encoding:
101 * "SegCount can be from 1 to 255"
102 */
103 assert(seg_count > 0 && seg_count <= 255);
104
105 /* handle RootPath || PrefixPath */
106 s = *segs;
107 while (*s == '\\' || *s == '^') {
108 build_append_byte(array, *s);
109 ++s;
110 }
111
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);
118 }
119 break;
120
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);
129
130 /* handle the 1st segment manually due to prefix/root path */
131 build_append_nameseg(array, s);
132
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;
138 }
139 break;
140 }
141 g_strfreev(segs);
142}
143
144void build_append_namestring(GArray *array, const char *format, ...)
145{
146 va_list ap;
147
148 va_start(ap, format);
149 build_append_namestringv(array, format, ap);
150 va_end(ap);
151}
152
19934e0e
IM
153/* 5.4 Definition Block Encoding */
154enum {
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,
159};
160
661875e9 161void build_prepend_package_length(GArray *package)
19934e0e
IM
162{
163 uint8_t byte;
164 unsigned length = package->len;
165 unsigned length_bytes;
166
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;
175 }
176
19934e0e
IM
177 /* PkgLength is the length of the inclusive length of the data. */
178 length += length_bytes;
179
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 */
200 }
201 /*
202 * Most significant two bits of byte zero indicate how many following bytes
203 * are in PkgLength encoding.
204 */
205 byte = ((length_bytes - 1) << PACKAGE_LENGTH_1BYTE_SHIFT) | length;
206 build_prepend_byte(package, byte);
207}
208
661875e9 209void build_package(GArray *package, uint8_t op)
19934e0e 210{
661875e9 211 build_prepend_package_length(package);
19934e0e
IM
212 build_prepend_byte(package, op);
213}
214
215void build_extop_package(GArray *package, uint8_t op)
216{
661875e9 217 build_package(package, op);
19934e0e
IM
218 build_prepend_byte(package, 0x5B); /* ExtOpPrefix */
219}
220
295a515d 221static void build_append_int_noprefix(GArray *table, uint64_t value, int size)
19934e0e 222{
19934e0e
IM
223 int i;
224
19934e0e
IM
225 for (i = 0; i < size; ++i) {
226 build_append_byte(table, value & 0xFF);
227 value = value >> 8;
228 }
229}
230
295a515d 231void build_append_int(GArray *table, uint64_t value)
19934e0e
IM
232{
233 if (value == 0x00) {
234 build_append_byte(table, 0x00); /* ZeroOp */
235 } else if (value == 0x01) {
236 build_append_byte(table, 0x01); /* OneOp */
237 } else if (value <= 0xFF) {
295a515d
IM
238 build_append_byte(table, 0x0A); /* BytePrefix */
239 build_append_int_noprefix(table, value, 1);
19934e0e 240 } else if (value <= 0xFFFF) {
295a515d
IM
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);
19934e0e 246 } else {
295a515d
IM
247 build_append_byte(table, 0x0E); /* QWordPrefix */
248 build_append_int_noprefix(table, value, 8);
19934e0e
IM
249 }
250}
0f2707e4
IM
251
252static GPtrArray *alloc_list;
253
254static Aml *aml_alloc(void)
255{
256 Aml *var = g_new0(typeof(*var), 1);
257
258 g_ptr_array_add(alloc_list, var);
259 var->block_flags = AML_NO_OPCODE;
260 var->buf = build_alloc_array();
261 return var;
262}
263
3c054bd5
IM
264static Aml *aml_opcode(uint8_t op)
265{
266 Aml *var = aml_alloc();
267
268 var->op = op;
269 var->block_flags = AML_OPCODE;
270 return var;
271}
272
2ef7c27b
IM
273static Aml *aml_bundle(uint8_t op, AmlBlockFlags flags)
274{
275 Aml *var = aml_alloc();
276
277 var->op = op;
278 var->block_flags = flags;
279 return var;
280}
281
0f2707e4
IM
282static void aml_free(gpointer data)
283{
284 Aml *var = data;
285 build_free_array(var->buf);
286}
287
288Aml *init_aml_allocator(void)
289{
290 Aml *var;
291
292 assert(!alloc_list);
293 alloc_list = g_ptr_array_new_with_free_func(aml_free);
294 var = aml_alloc();
295 return var;
296}
297
298void free_aml_allocator(void)
299{
300 g_ptr_array_free(alloc_list, true);
301 alloc_list = 0;
302}
303
304/* pack data with DefBuffer encoding */
305static void build_buffer(GArray *array, uint8_t op)
306{
307 GArray *data = build_alloc_array();
308
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);
313}
314
315void aml_append(Aml *parent_ctx, Aml *child)
316{
317 switch (child->block_flags) {
318 case AML_OPCODE:
319 build_append_byte(parent_ctx->buf, child->op);
320 break;
321 case AML_EXT_PACKAGE:
322 build_extop_package(child->buf, child->op);
323 break;
324 case AML_PACKAGE:
325 build_package(child->buf, child->op);
326 break;
327 case AML_RES_TEMPLATE:
328 build_append_byte(child->buf, 0x79); /* EndTag */
329 /*
330 * checksum operations are treated as succeeded if checksum
331 * field is zero. [ACPI Spec 1.0b, 6.4.2.8 End Tag]
332 */
333 build_append_byte(child->buf, 0);
334 /* fall through, to pack resources in buffer */
335 case AML_BUFFER:
336 build_buffer(child->buf, child->op);
337 break;
338 case AML_NO_OPCODE:
339 break;
340 default:
341 assert(0);
342 break;
343 }
344 build_append_array(parent_ctx->buf, child->buf);
345}
2ef7c27b
IM
346
347/* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefScope */
348Aml *aml_scope(const char *name_format, ...)
349{
350 va_list ap;
351 Aml *var = aml_bundle(0x10 /* ScopeOp */, AML_PACKAGE);
352 va_start(ap, name_format);
353 build_append_namestringv(var->buf, name_format, ap);
354 va_end(ap);
355 return var;
356}
be06ebd0 357
b25af5ad
IM
358/* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefReturn */
359Aml *aml_return(Aml *val)
360{
361 Aml *var = aml_opcode(0xA4 /* ReturnOp */);
362 aml_append(var, val);
363 return var;
364}
365
295a515d
IM
366/*
367 * ACPI 1.0b: 16.2.3 Data Objects Encoding:
368 * encodes: ByteConst, WordConst, DWordConst, QWordConst, ZeroOp, OneOp
369 */
370Aml *aml_int(const uint64_t val)
371{
372 Aml *var = aml_alloc();
373 build_append_int(var->buf, val);
374 return var;
375}
376
3c054bd5
IM
377/*
378 * helper to construct NameString, which returns Aml object
379 * for using with aml_append or other aml_* terms
380 */
381Aml *aml_name(const char *name_format, ...)
382{
383 va_list ap;
384 Aml *var = aml_alloc();
385 va_start(ap, name_format);
386 build_append_namestringv(var->buf, name_format, ap);
387 va_end(ap);
388 return var;
389}
390
391/* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefName */
392Aml *aml_name_decl(const char *name, Aml *val)
393{
394 Aml *var = aml_opcode(0x08 /* NameOp */);
395 build_append_namestring(var->buf, "%s", name);
396 aml_append(var, val);
397 return var;
398}
399
7193f3a6
IM
400/* ACPI 1.0b: 16.2.6.1 Arg Objects Encoding */
401Aml *aml_arg(int pos)
402{
403 Aml *var;
404 uint8_t op = 0x68 /* ARG0 op */ + pos;
405
406 assert(pos <= 6);
407 var = aml_opcode(op);
408 return var;
409}
410
c263b3f7
IM
411/* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefStore */
412Aml *aml_store(Aml *val, Aml *target)
413{
414 Aml *var = aml_opcode(0x70 /* StoreOp */);
415 aml_append(var, val);
416 aml_append(var, target);
417 return var;
418}
419
926f5aae
IM
420/* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAnd */
421Aml *aml_and(Aml *arg1, Aml *arg2)
422{
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 */);
427 return var;
428}
429
34189453
IM
430/* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefNotify */
431Aml *aml_notify(Aml *arg1, Aml *arg2)
432{
433 Aml *var = aml_opcode(0x86 /* NotifyOp */);
434 aml_append(var, arg1);
435 aml_append(var, arg2);
436 return var;
437}
438
32acac9e
IM
439/* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */
440Aml *aml_if(Aml *predicate)
441{
442 Aml *var = aml_bundle(0xA0 /* IfOp */, AML_PACKAGE);
443 aml_append(var, predicate);
444 return var;
445}
446
ea2407d7
IM
447/* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */
448Aml *aml_method(const char *name, int arg_count)
449{
450 Aml *var = aml_bundle(0x14 /* MethodOp */, AML_PACKAGE);
451 build_append_namestring(var->buf, "%s", name);
452 build_append_byte(var->buf, arg_count); /* MethodFlags: ArgCount */
453 return var;
454}
455
be06ebd0
IM
456/* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */
457Aml *aml_device(const char *name_format, ...)
458{
459 va_list ap;
460 Aml *var = aml_bundle(0x82 /* DeviceOp */, AML_EXT_PACKAGE);
461 va_start(ap, name_format);
462 build_append_namestringv(var->buf, name_format, ap);
463 va_end(ap);
464 return var;
465}
This page took 0.076885 seconds and 4 git commands to generate.