]> Git Repo - qemu.git/blame - hw/acpi/aml-build.c
acpi: add aml_operation_region() 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
19fff2d4
IM
161void
162build_prepend_package_length(GArray *package, unsigned length, bool incl_self)
19934e0e
IM
163{
164 uint8_t byte;
19934e0e
IM
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
19fff2d4
IM
177 /*
178 * NamedField uses PkgLength encoding but it doesn't include length
179 * of PkgLength itself.
180 */
181 if (incl_self) {
182 /*
183 * PkgLength is the length of the inclusive length of the data
184 * and PkgLength's length itself when used for terms with
185 * explitit length.
186 */
187 length += length_bytes;
188 }
19934e0e
IM
189
190 switch (length_bytes) {
191 case 1:
192 byte = length;
193 build_prepend_byte(package, byte);
194 return;
195 case 4:
196 byte = length >> PACKAGE_LENGTH_4BYTE_SHIFT;
197 build_prepend_byte(package, byte);
198 length &= (1 << PACKAGE_LENGTH_4BYTE_SHIFT) - 1;
199 /* fall through */
200 case 3:
201 byte = length >> PACKAGE_LENGTH_3BYTE_SHIFT;
202 build_prepend_byte(package, byte);
203 length &= (1 << PACKAGE_LENGTH_3BYTE_SHIFT) - 1;
204 /* fall through */
205 case 2:
206 byte = length >> PACKAGE_LENGTH_2BYTE_SHIFT;
207 build_prepend_byte(package, byte);
208 length &= (1 << PACKAGE_LENGTH_2BYTE_SHIFT) - 1;
209 /* fall through */
210 }
211 /*
212 * Most significant two bits of byte zero indicate how many following bytes
213 * are in PkgLength encoding.
214 */
215 byte = ((length_bytes - 1) << PACKAGE_LENGTH_1BYTE_SHIFT) | length;
216 build_prepend_byte(package, byte);
217}
218
661875e9 219void build_package(GArray *package, uint8_t op)
19934e0e 220{
19fff2d4 221 build_prepend_package_length(package, package->len, true);
19934e0e
IM
222 build_prepend_byte(package, op);
223}
224
225void build_extop_package(GArray *package, uint8_t op)
226{
661875e9 227 build_package(package, op);
19934e0e
IM
228 build_prepend_byte(package, 0x5B); /* ExtOpPrefix */
229}
230
295a515d 231static void build_append_int_noprefix(GArray *table, uint64_t value, int size)
19934e0e 232{
19934e0e
IM
233 int i;
234
19934e0e
IM
235 for (i = 0; i < size; ++i) {
236 build_append_byte(table, value & 0xFF);
237 value = value >> 8;
238 }
239}
240
295a515d 241void build_append_int(GArray *table, uint64_t value)
19934e0e
IM
242{
243 if (value == 0x00) {
244 build_append_byte(table, 0x00); /* ZeroOp */
245 } else if (value == 0x01) {
246 build_append_byte(table, 0x01); /* OneOp */
247 } else if (value <= 0xFF) {
295a515d
IM
248 build_append_byte(table, 0x0A); /* BytePrefix */
249 build_append_int_noprefix(table, value, 1);
19934e0e 250 } else if (value <= 0xFFFF) {
295a515d
IM
251 build_append_byte(table, 0x0B); /* WordPrefix */
252 build_append_int_noprefix(table, value, 2);
253 } else if (value <= 0xFFFFFFFF) {
254 build_append_byte(table, 0x0C); /* DWordPrefix */
255 build_append_int_noprefix(table, value, 4);
19934e0e 256 } else {
295a515d
IM
257 build_append_byte(table, 0x0E); /* QWordPrefix */
258 build_append_int_noprefix(table, value, 8);
19934e0e
IM
259 }
260}
0f2707e4
IM
261
262static GPtrArray *alloc_list;
263
264static Aml *aml_alloc(void)
265{
266 Aml *var = g_new0(typeof(*var), 1);
267
268 g_ptr_array_add(alloc_list, var);
269 var->block_flags = AML_NO_OPCODE;
270 var->buf = build_alloc_array();
271 return var;
272}
273
3c054bd5
IM
274static Aml *aml_opcode(uint8_t op)
275{
276 Aml *var = aml_alloc();
277
278 var->op = op;
279 var->block_flags = AML_OPCODE;
280 return var;
281}
282
2ef7c27b
IM
283static Aml *aml_bundle(uint8_t op, AmlBlockFlags flags)
284{
285 Aml *var = aml_alloc();
286
287 var->op = op;
288 var->block_flags = flags;
289 return var;
290}
291
0f2707e4
IM
292static void aml_free(gpointer data)
293{
294 Aml *var = data;
295 build_free_array(var->buf);
296}
297
298Aml *init_aml_allocator(void)
299{
300 Aml *var;
301
302 assert(!alloc_list);
303 alloc_list = g_ptr_array_new_with_free_func(aml_free);
304 var = aml_alloc();
305 return var;
306}
307
308void free_aml_allocator(void)
309{
310 g_ptr_array_free(alloc_list, true);
311 alloc_list = 0;
312}
313
314/* pack data with DefBuffer encoding */
315static void build_buffer(GArray *array, uint8_t op)
316{
317 GArray *data = build_alloc_array();
318
319 build_append_int(data, array->len);
320 g_array_prepend_vals(array, data->data, data->len);
321 build_free_array(data);
322 build_package(array, op);
323}
324
325void aml_append(Aml *parent_ctx, Aml *child)
326{
327 switch (child->block_flags) {
328 case AML_OPCODE:
329 build_append_byte(parent_ctx->buf, child->op);
330 break;
331 case AML_EXT_PACKAGE:
332 build_extop_package(child->buf, child->op);
333 break;
334 case AML_PACKAGE:
335 build_package(child->buf, child->op);
336 break;
337 case AML_RES_TEMPLATE:
338 build_append_byte(child->buf, 0x79); /* EndTag */
339 /*
340 * checksum operations are treated as succeeded if checksum
341 * field is zero. [ACPI Spec 1.0b, 6.4.2.8 End Tag]
342 */
343 build_append_byte(child->buf, 0);
344 /* fall through, to pack resources in buffer */
345 case AML_BUFFER:
346 build_buffer(child->buf, child->op);
347 break;
348 case AML_NO_OPCODE:
349 break;
350 default:
351 assert(0);
352 break;
353 }
354 build_append_array(parent_ctx->buf, child->buf);
355}
2ef7c27b
IM
356
357/* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefScope */
358Aml *aml_scope(const char *name_format, ...)
359{
360 va_list ap;
361 Aml *var = aml_bundle(0x10 /* ScopeOp */, AML_PACKAGE);
362 va_start(ap, name_format);
363 build_append_namestringv(var->buf, name_format, ap);
364 va_end(ap);
365 return var;
366}
be06ebd0 367
b25af5ad
IM
368/* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefReturn */
369Aml *aml_return(Aml *val)
370{
371 Aml *var = aml_opcode(0xA4 /* ReturnOp */);
372 aml_append(var, val);
373 return var;
374}
375
295a515d
IM
376/*
377 * ACPI 1.0b: 16.2.3 Data Objects Encoding:
378 * encodes: ByteConst, WordConst, DWordConst, QWordConst, ZeroOp, OneOp
379 */
380Aml *aml_int(const uint64_t val)
381{
382 Aml *var = aml_alloc();
383 build_append_int(var->buf, val);
384 return var;
385}
386
3c054bd5
IM
387/*
388 * helper to construct NameString, which returns Aml object
389 * for using with aml_append or other aml_* terms
390 */
391Aml *aml_name(const char *name_format, ...)
392{
393 va_list ap;
394 Aml *var = aml_alloc();
395 va_start(ap, name_format);
396 build_append_namestringv(var->buf, name_format, ap);
397 va_end(ap);
398 return var;
399}
400
401/* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefName */
402Aml *aml_name_decl(const char *name, Aml *val)
403{
404 Aml *var = aml_opcode(0x08 /* NameOp */);
405 build_append_namestring(var->buf, "%s", name);
406 aml_append(var, val);
407 return var;
408}
409
7193f3a6
IM
410/* ACPI 1.0b: 16.2.6.1 Arg Objects Encoding */
411Aml *aml_arg(int pos)
412{
413 Aml *var;
414 uint8_t op = 0x68 /* ARG0 op */ + pos;
415
416 assert(pos <= 6);
417 var = aml_opcode(op);
418 return var;
419}
420
c263b3f7
IM
421/* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefStore */
422Aml *aml_store(Aml *val, Aml *target)
423{
424 Aml *var = aml_opcode(0x70 /* StoreOp */);
425 aml_append(var, val);
426 aml_append(var, target);
427 return var;
428}
429
926f5aae
IM
430/* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAnd */
431Aml *aml_and(Aml *arg1, Aml *arg2)
432{
433 Aml *var = aml_opcode(0x7B /* AndOp */);
434 aml_append(var, arg1);
435 aml_append(var, arg2);
436 build_append_int(var->buf, 0x00 /* NullNameOp */);
437 return var;
438}
439
34189453
IM
440/* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefNotify */
441Aml *aml_notify(Aml *arg1, Aml *arg2)
442{
443 Aml *var = aml_opcode(0x86 /* NotifyOp */);
444 aml_append(var, arg1);
445 aml_append(var, arg2);
446 return var;
447}
448
3f3992b7
IM
449/* helper to call method with 1 argument */
450Aml *aml_call1(const char *method, Aml *arg1)
451{
452 Aml *var = aml_alloc();
453 build_append_namestring(var->buf, "%s", method);
454 aml_append(var, arg1);
455 return var;
456}
457
458/* helper to call method with 2 arguments */
459Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2)
460{
461 Aml *var = aml_alloc();
462 build_append_namestring(var->buf, "%s", method);
463 aml_append(var, arg1);
464 aml_append(var, arg2);
465 return var;
466}
467
468/* helper to call method with 3 arguments */
469Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3)
470{
471 Aml *var = aml_alloc();
472 build_append_namestring(var->buf, "%s", method);
473 aml_append(var, arg1);
474 aml_append(var, arg2);
475 aml_append(var, arg3);
476 return var;
477}
478
479/* helper to call method with 4 arguments */
480Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4)
481{
482 Aml *var = aml_alloc();
483 build_append_namestring(var->buf, "%s", method);
484 aml_append(var, arg1);
485 aml_append(var, arg2);
486 aml_append(var, arg3);
487 aml_append(var, arg4);
488 return var;
489}
490
52fa397c
IM
491/* ACPI 1.0b: 6.4.2.5 I/O Port Descriptor */
492Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base,
493 uint8_t aln, uint8_t len)
494{
495 Aml *var = aml_alloc();
496 build_append_byte(var->buf, 0x47); /* IO port descriptor */
497 build_append_byte(var->buf, dec);
498 build_append_byte(var->buf, min_base & 0xff);
499 build_append_byte(var->buf, (min_base >> 8) & 0xff);
500 build_append_byte(var->buf, max_base & 0xff);
501 build_append_byte(var->buf, (max_base >> 8) & 0xff);
502 build_append_byte(var->buf, aln);
503 build_append_byte(var->buf, len);
504 return var;
505}
506
32acac9e
IM
507/* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */
508Aml *aml_if(Aml *predicate)
509{
510 Aml *var = aml_bundle(0xA0 /* IfOp */, AML_PACKAGE);
511 aml_append(var, predicate);
512 return var;
513}
514
ea2407d7
IM
515/* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */
516Aml *aml_method(const char *name, int arg_count)
517{
518 Aml *var = aml_bundle(0x14 /* MethodOp */, AML_PACKAGE);
519 build_append_namestring(var->buf, "%s", name);
520 build_append_byte(var->buf, arg_count); /* MethodFlags: ArgCount */
521 return var;
522}
523
be06ebd0
IM
524/* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */
525Aml *aml_device(const char *name_format, ...)
526{
527 va_list ap;
528 Aml *var = aml_bundle(0x82 /* DeviceOp */, AML_EXT_PACKAGE);
529 va_start(ap, name_format);
530 build_append_namestringv(var->buf, name_format, ap);
531 va_end(ap);
532 return var;
533}
3bfa74a7 534
ad4a80bc
IM
535/* ACPI 1.0b: 6.4.1 ASL Macros for Resource Descriptors */
536Aml *aml_resource_template(void)
537{
538 /* ResourceTemplate is a buffer of Resources with EndTag at the end */
539 Aml *var = aml_bundle(0x11 /* BufferOp */, AML_RES_TEMPLATE);
540 return var;
541}
542
04b8da54
IM
543/* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefBuffer */
544Aml *aml_buffer(void)
545{
546 Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER);
547 return var;
548}
549
3bfa74a7
IM
550/* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefPackage */
551Aml *aml_package(uint8_t num_elements)
552{
553 Aml *var = aml_bundle(0x12 /* PackageOp */, AML_PACKAGE);
554 build_append_byte(var->buf, num_elements);
555 return var;
556}
31127938
IM
557
558/* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefOpRegion */
559Aml *aml_operation_region(const char *name, AmlRegionSpace rs,
560 uint32_t offset, uint32_t len)
561{
562 Aml *var = aml_alloc();
563 build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
564 build_append_byte(var->buf, 0x80); /* OpRegionOp */
565 build_append_namestring(var->buf, "%s", name);
566 build_append_byte(var->buf, rs);
567 build_append_int(var->buf, offset);
568 build_append_int(var->buf, len);
569 return var;
570}
This page took 0.087959 seconds and 4 git commands to generate.