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 <mst@redhat.com>
6  * Author: Igor Mammedov <imammedo@redhat.com>
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 "qemu/osdep.h"
23 #include <glib/gprintf.h>
24 #include "hw/acpi/aml-build.h"
25 #include "qemu/bswap.h"
26 #include "qemu/bitops.h"
27 #include "sysemu/numa.h"
28 
build_alloc_array(void)29 static GArray *build_alloc_array(void)
30 {
31     return g_array_new(false, true /* clear */, 1);
32 }
33 
build_free_array(GArray * array)34 static void build_free_array(GArray *array)
35 {
36     g_array_free(array, true);
37 }
38 
build_prepend_byte(GArray * array,uint8_t val)39 static void build_prepend_byte(GArray *array, uint8_t val)
40 {
41     g_array_prepend_val(array, val);
42 }
43 
build_append_byte(GArray * array,uint8_t val)44 static void build_append_byte(GArray *array, uint8_t val)
45 {
46     g_array_append_val(array, val);
47 }
48 
build_append_array(GArray * array,GArray * val)49 static void build_append_array(GArray *array, GArray *val)
50 {
51     g_array_append_vals(array, val->data, val->len);
52 }
53 
54 #define ACPI_NAMESEG_LEN 4
55 
56 static void
build_append_nameseg(GArray * array,const char * seg)57 build_append_nameseg(GArray *array, const char *seg)
58 {
59     int len;
60 
61     len = strlen(seg);
62     assert(len <= ACPI_NAMESEG_LEN);
63 
64     g_array_append_vals(array, seg, len);
65     /* Pad up to ACPI_NAMESEG_LEN characters if necessary. */
66     g_array_append_vals(array, "____", ACPI_NAMESEG_LEN - len);
67 }
68 
69 static void GCC_FMT_ATTR(2, 0)
build_append_namestringv(GArray * array,const char * format,va_list ap)70 build_append_namestringv(GArray *array, const char *format, va_list ap)
71 {
72     char *s;
73     char **segs;
74     char **segs_iter;
75     int seg_count = 0;
76 
77     s = g_strdup_vprintf(format, ap);
78     segs = g_strsplit(s, ".", 0);
79     g_free(s);
80 
81     /* count segments */
82     segs_iter = segs;
83     while (*segs_iter) {
84         ++segs_iter;
85         ++seg_count;
86     }
87     /*
88      * ACPI 5.0 spec: 20.2.2 Name Objects Encoding:
89      * "SegCount can be from 1 to 255"
90      */
91     assert(seg_count > 0 && seg_count <= 255);
92 
93     /* handle RootPath || PrefixPath */
94     s = *segs;
95     while (*s == '\\' || *s == '^') {
96         build_append_byte(array, *s);
97         ++s;
98     }
99 
100     switch (seg_count) {
101     case 1:
102         if (!*s) {
103             build_append_byte(array, 0x00); /* NullName */
104         } else {
105             build_append_nameseg(array, s);
106         }
107         break;
108 
109     case 2:
110         build_append_byte(array, 0x2E); /* DualNamePrefix */
111         build_append_nameseg(array, s);
112         build_append_nameseg(array, segs[1]);
113         break;
114     default:
115         build_append_byte(array, 0x2F); /* MultiNamePrefix */
116         build_append_byte(array, seg_count);
117 
118         /* handle the 1st segment manually due to prefix/root path */
119         build_append_nameseg(array, s);
120 
121         /* add the rest of segments */
122         segs_iter = segs + 1;
123         while (*segs_iter) {
124             build_append_nameseg(array, *segs_iter);
125             ++segs_iter;
126         }
127         break;
128     }
129     g_strfreev(segs);
130 }
131 
132 GCC_FMT_ATTR(2, 3)
build_append_namestring(GArray * array,const char * format,...)133 static void build_append_namestring(GArray *array, const char *format, ...)
134 {
135     va_list ap;
136 
137     va_start(ap, format);
138     build_append_namestringv(array, format, ap);
139     va_end(ap);
140 }
141 
142 /* 5.4 Definition Block Encoding */
143 enum {
144     PACKAGE_LENGTH_1BYTE_SHIFT = 6, /* Up to 63 - use extra 2 bits. */
145     PACKAGE_LENGTH_2BYTE_SHIFT = 4,
146     PACKAGE_LENGTH_3BYTE_SHIFT = 12,
147     PACKAGE_LENGTH_4BYTE_SHIFT = 20,
148 };
149 
150 static void
build_prepend_package_length(GArray * package,unsigned length,bool incl_self)151 build_prepend_package_length(GArray *package, unsigned length, bool incl_self)
152 {
153     uint8_t byte;
154     unsigned length_bytes;
155 
156     if (length + 1 < (1 << PACKAGE_LENGTH_1BYTE_SHIFT)) {
157         length_bytes = 1;
158     } else if (length + 2 < (1 << PACKAGE_LENGTH_3BYTE_SHIFT)) {
159         length_bytes = 2;
160     } else if (length + 3 < (1 << PACKAGE_LENGTH_4BYTE_SHIFT)) {
161         length_bytes = 3;
162     } else {
163         length_bytes = 4;
164     }
165 
166     /*
167      * NamedField uses PkgLength encoding but it doesn't include length
168      * of PkgLength itself.
169      */
170     if (incl_self) {
171         /*
172          * PkgLength is the length of the inclusive length of the data
173          * and PkgLength's length itself when used for terms with
174          * explitit length.
175          */
176         length += length_bytes;
177     }
178 
179     switch (length_bytes) {
180     case 1:
181         byte = length;
182         build_prepend_byte(package, byte);
183         return;
184     case 4:
185         byte = length >> PACKAGE_LENGTH_4BYTE_SHIFT;
186         build_prepend_byte(package, byte);
187         length &= (1 << PACKAGE_LENGTH_4BYTE_SHIFT) - 1;
188         /* fall through */
189     case 3:
190         byte = length >> PACKAGE_LENGTH_3BYTE_SHIFT;
191         build_prepend_byte(package, byte);
192         length &= (1 << PACKAGE_LENGTH_3BYTE_SHIFT) - 1;
193         /* fall through */
194     case 2:
195         byte = length >> PACKAGE_LENGTH_2BYTE_SHIFT;
196         build_prepend_byte(package, byte);
197         length &= (1 << PACKAGE_LENGTH_2BYTE_SHIFT) - 1;
198         /* fall through */
199     }
200     /*
201      * Most significant two bits of byte zero indicate how many following bytes
202      * are in PkgLength encoding.
203      */
204     byte = ((length_bytes - 1) << PACKAGE_LENGTH_1BYTE_SHIFT) | length;
205     build_prepend_byte(package, byte);
206 }
207 
208 static void
build_append_pkg_length(GArray * array,unsigned length,bool incl_self)209 build_append_pkg_length(GArray *array, unsigned length, bool incl_self)
210 {
211     GArray *tmp = build_alloc_array();
212 
213     build_prepend_package_length(tmp, length, incl_self);
214     build_append_array(array, tmp);
215     build_free_array(tmp);
216 }
217 
build_package(GArray * package,uint8_t op)218 static void build_package(GArray *package, uint8_t op)
219 {
220     build_prepend_package_length(package, package->len, true);
221     build_prepend_byte(package, op);
222 }
223 
build_extop_package(GArray * package,uint8_t op)224 static void build_extop_package(GArray *package, uint8_t op)
225 {
226     build_package(package, op);
227     build_prepend_byte(package, 0x5B); /* ExtOpPrefix */
228 }
229 
build_append_int_noprefix(GArray * table,uint64_t value,int size)230 void build_append_int_noprefix(GArray *table, uint64_t value, int size)
231 {
232     int i;
233 
234     for (i = 0; i < size; ++i) {
235         build_append_byte(table, value & 0xFF);
236         value = value >> 8;
237     }
238 }
239 
build_append_int(GArray * table,uint64_t value)240 static void build_append_int(GArray *table, uint64_t value)
241 {
242     if (value == 0x00) {
243         build_append_byte(table, 0x00); /* ZeroOp */
244     } else if (value == 0x01) {
245         build_append_byte(table, 0x01); /* OneOp */
246     } else if (value <= 0xFF) {
247         build_append_byte(table, 0x0A); /* BytePrefix */
248         build_append_int_noprefix(table, value, 1);
249     } else if (value <= 0xFFFF) {
250         build_append_byte(table, 0x0B); /* WordPrefix */
251         build_append_int_noprefix(table, value, 2);
252     } else if (value <= 0xFFFFFFFF) {
253         build_append_byte(table, 0x0C); /* DWordPrefix */
254         build_append_int_noprefix(table, value, 4);
255     } else {
256         build_append_byte(table, 0x0E); /* QWordPrefix */
257         build_append_int_noprefix(table, value, 8);
258     }
259 }
260 
261 /* Generic Address Structure (GAS)
262  * ACPI 2.0/3.0: 5.2.3.1 Generic Address Structure
263  * 2.0 compat note:
264  *    @access_width must be 0, see ACPI 2.0:Table 5-1
265  */
build_append_gas(GArray * table,AmlAddressSpace as,uint8_t bit_width,uint8_t bit_offset,uint8_t access_width,uint64_t address)266 void build_append_gas(GArray *table, AmlAddressSpace as,
267                       uint8_t bit_width, uint8_t bit_offset,
268                       uint8_t access_width, uint64_t address)
269 {
270     build_append_int_noprefix(table, as, 1);
271     build_append_int_noprefix(table, bit_width, 1);
272     build_append_int_noprefix(table, bit_offset, 1);
273     build_append_int_noprefix(table, access_width, 1);
274     build_append_int_noprefix(table, address, 8);
275 }
276 
277 /*
278  * Build NAME(XXXX, 0x00000000) where 0x00000000 is encoded as a dword,
279  * and return the offset to 0x00000000 for runtime patching.
280  *
281  * Warning: runtime patching is best avoided. Only use this as
282  * a replacement for DataTableRegion (for guests that don't
283  * support it).
284  */
285 int
build_append_named_dword(GArray * array,const char * name_format,...)286 build_append_named_dword(GArray *array, const char *name_format, ...)
287 {
288     int offset;
289     va_list ap;
290 
291     build_append_byte(array, 0x08); /* NameOp */
292     va_start(ap, name_format);
293     build_append_namestringv(array, name_format, ap);
294     va_end(ap);
295 
296     build_append_byte(array, 0x0C); /* DWordPrefix */
297 
298     offset = array->len;
299     build_append_int_noprefix(array, 0x00000000, 4);
300     assert(array->len == offset + 4);
301 
302     return offset;
303 }
304 
305 static GPtrArray *alloc_list;
306 
aml_alloc(void)307 static Aml *aml_alloc(void)
308 {
309     Aml *var = g_new0(typeof(*var), 1);
310 
311     g_ptr_array_add(alloc_list, var);
312     var->block_flags = AML_NO_OPCODE;
313     var->buf = build_alloc_array();
314     return var;
315 }
316 
aml_opcode(uint8_t op)317 static Aml *aml_opcode(uint8_t op)
318 {
319     Aml *var = aml_alloc();
320 
321     var->op  = op;
322     var->block_flags = AML_OPCODE;
323     return var;
324 }
325 
aml_bundle(uint8_t op,AmlBlockFlags flags)326 static Aml *aml_bundle(uint8_t op, AmlBlockFlags flags)
327 {
328     Aml *var = aml_alloc();
329 
330     var->op  = op;
331     var->block_flags = flags;
332     return var;
333 }
334 
aml_free(gpointer data,gpointer user_data)335 static void aml_free(gpointer data, gpointer user_data)
336 {
337     Aml *var = data;
338     build_free_array(var->buf);
339     g_free(var);
340 }
341 
init_aml_allocator(void)342 Aml *init_aml_allocator(void)
343 {
344     assert(!alloc_list);
345     alloc_list = g_ptr_array_new();
346     return aml_alloc();
347 }
348 
free_aml_allocator(void)349 void free_aml_allocator(void)
350 {
351     g_ptr_array_foreach(alloc_list, aml_free, NULL);
352     g_ptr_array_free(alloc_list, true);
353     alloc_list = 0;
354 }
355 
356 /* pack data with DefBuffer encoding */
build_buffer(GArray * array,uint8_t op)357 static void build_buffer(GArray *array, uint8_t op)
358 {
359     GArray *data = build_alloc_array();
360 
361     build_append_int(data, array->len);
362     g_array_prepend_vals(array, data->data, data->len);
363     build_free_array(data);
364     build_package(array, op);
365 }
366 
aml_append(Aml * parent_ctx,Aml * child)367 void aml_append(Aml *parent_ctx, Aml *child)
368 {
369     GArray *buf = build_alloc_array();
370     build_append_array(buf, child->buf);
371 
372     switch (child->block_flags) {
373     case AML_OPCODE:
374         build_append_byte(parent_ctx->buf, child->op);
375         break;
376     case AML_EXT_PACKAGE:
377         build_extop_package(buf, child->op);
378         break;
379     case AML_PACKAGE:
380         build_package(buf, child->op);
381         break;
382     case AML_RES_TEMPLATE:
383         build_append_byte(buf, 0x79); /* EndTag */
384         /*
385          * checksum operations are treated as succeeded if checksum
386          * field is zero. [ACPI Spec 1.0b, 6.4.2.8 End Tag]
387          */
388         build_append_byte(buf, 0);
389         /* fall through, to pack resources in buffer */
390     case AML_BUFFER:
391         build_buffer(buf, child->op);
392         break;
393     case AML_NO_OPCODE:
394         break;
395     default:
396         assert(0);
397         break;
398     }
399     build_append_array(parent_ctx->buf, buf);
400     build_free_array(buf);
401 }
402 
403 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefScope */
aml_scope(const char * name_format,...)404 Aml *aml_scope(const char *name_format, ...)
405 {
406     va_list ap;
407     Aml *var = aml_bundle(0x10 /* ScopeOp */, AML_PACKAGE);
408     va_start(ap, name_format);
409     build_append_namestringv(var->buf, name_format, ap);
410     va_end(ap);
411     return var;
412 }
413 
414 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefReturn */
aml_return(Aml * val)415 Aml *aml_return(Aml *val)
416 {
417     Aml *var = aml_opcode(0xA4 /* ReturnOp */);
418     aml_append(var, val);
419     return var;
420 }
421 
422 /* ACPI 1.0b: 16.2.6.3 Debug Objects Encoding: DebugObj */
aml_debug(void)423 Aml *aml_debug(void)
424 {
425     Aml *var = aml_alloc();
426     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
427     build_append_byte(var->buf, 0x31); /* DebugOp */
428     return var;
429 }
430 
431 /*
432  * ACPI 1.0b: 16.2.3 Data Objects Encoding:
433  * encodes: ByteConst, WordConst, DWordConst, QWordConst, ZeroOp, OneOp
434  */
aml_int(const uint64_t val)435 Aml *aml_int(const uint64_t val)
436 {
437     Aml *var = aml_alloc();
438     build_append_int(var->buf, val);
439     return var;
440 }
441 
442 /*
443  * helper to construct NameString, which returns Aml object
444  * for using with aml_append or other aml_* terms
445  */
aml_name(const char * name_format,...)446 Aml *aml_name(const char *name_format, ...)
447 {
448     va_list ap;
449     Aml *var = aml_alloc();
450     va_start(ap, name_format);
451     build_append_namestringv(var->buf, name_format, ap);
452     va_end(ap);
453     return var;
454 }
455 
456 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefName */
aml_name_decl(const char * name,Aml * val)457 Aml *aml_name_decl(const char *name, Aml *val)
458 {
459     Aml *var = aml_opcode(0x08 /* NameOp */);
460     build_append_namestring(var->buf, "%s", name);
461     aml_append(var, val);
462     return var;
463 }
464 
465 /* ACPI 1.0b: 16.2.6.1 Arg Objects Encoding */
aml_arg(int pos)466 Aml *aml_arg(int pos)
467 {
468     uint8_t op = 0x68 /* ARG0 op */ + pos;
469 
470     assert(pos <= 6);
471     return aml_opcode(op);
472 }
473 
474 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToInteger */
aml_to_integer(Aml * arg)475 Aml *aml_to_integer(Aml *arg)
476 {
477     Aml *var = aml_opcode(0x99 /* ToIntegerOp */);
478     aml_append(var, arg);
479     build_append_byte(var->buf, 0x00 /* NullNameOp */);
480     return var;
481 }
482 
483 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToHexString */
aml_to_hexstring(Aml * src,Aml * dst)484 Aml *aml_to_hexstring(Aml *src, Aml *dst)
485 {
486     Aml *var = aml_opcode(0x98 /* ToHexStringOp */);
487     aml_append(var, src);
488     if (dst) {
489         aml_append(var, dst);
490     } else {
491         build_append_byte(var->buf, 0x00 /* NullNameOp */);
492     }
493     return var;
494 }
495 
496 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToBuffer */
aml_to_buffer(Aml * src,Aml * dst)497 Aml *aml_to_buffer(Aml *src, Aml *dst)
498 {
499     Aml *var = aml_opcode(0x96 /* ToBufferOp */);
500     aml_append(var, src);
501     if (dst) {
502         aml_append(var, dst);
503     } else {
504         build_append_byte(var->buf, 0x00 /* NullNameOp */);
505     }
506     return var;
507 }
508 
509 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefStore */
aml_store(Aml * val,Aml * target)510 Aml *aml_store(Aml *val, Aml *target)
511 {
512     Aml *var = aml_opcode(0x70 /* StoreOp */);
513     aml_append(var, val);
514     aml_append(var, target);
515     return var;
516 }
517 
518 /**
519  * build_opcode_2arg_dst:
520  * @op: 1-byte opcode
521  * @arg1: 1st operand
522  * @arg2: 2nd operand
523  * @dst: optional target to store to, set to NULL if it's not required
524  *
525  * An internal helper to compose AML terms that have
526  *   "Op Operand Operand Target"
527  * pattern.
528  *
529  * Returns: The newly allocated and composed according to patter Aml object.
530  */
531 static Aml *
build_opcode_2arg_dst(uint8_t op,Aml * arg1,Aml * arg2,Aml * dst)532 build_opcode_2arg_dst(uint8_t op, Aml *arg1, Aml *arg2, Aml *dst)
533 {
534     Aml *var = aml_opcode(op);
535     aml_append(var, arg1);
536     aml_append(var, arg2);
537     if (dst) {
538         aml_append(var, dst);
539     } else {
540         build_append_byte(var->buf, 0x00 /* NullNameOp */);
541     }
542     return var;
543 }
544 
545 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAnd */
aml_and(Aml * arg1,Aml * arg2,Aml * dst)546 Aml *aml_and(Aml *arg1, Aml *arg2, Aml *dst)
547 {
548     return build_opcode_2arg_dst(0x7B /* AndOp */, arg1, arg2, dst);
549 }
550 
551 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefOr */
aml_or(Aml * arg1,Aml * arg2,Aml * dst)552 Aml *aml_or(Aml *arg1, Aml *arg2, Aml *dst)
553 {
554     return build_opcode_2arg_dst(0x7D /* OrOp */, arg1, arg2, dst);
555 }
556 
557 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLOr */
aml_lor(Aml * arg1,Aml * arg2)558 Aml *aml_lor(Aml *arg1, Aml *arg2)
559 {
560     Aml *var = aml_opcode(0x91 /* LOrOp */);
561     aml_append(var, arg1);
562     aml_append(var, arg2);
563     return var;
564 }
565 
566 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftLeft */
aml_shiftleft(Aml * arg1,Aml * count)567 Aml *aml_shiftleft(Aml *arg1, Aml *count)
568 {
569     return build_opcode_2arg_dst(0x79 /* ShiftLeftOp */, arg1, count, NULL);
570 }
571 
572 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftRight */
aml_shiftright(Aml * arg1,Aml * count,Aml * dst)573 Aml *aml_shiftright(Aml *arg1, Aml *count, Aml *dst)
574 {
575     return build_opcode_2arg_dst(0x7A /* ShiftRightOp */, arg1, count, dst);
576 }
577 
578 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLLess */
aml_lless(Aml * arg1,Aml * arg2)579 Aml *aml_lless(Aml *arg1, Aml *arg2)
580 {
581     Aml *var = aml_opcode(0x95 /* LLessOp */);
582     aml_append(var, arg1);
583     aml_append(var, arg2);
584     return var;
585 }
586 
587 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAdd */
aml_add(Aml * arg1,Aml * arg2,Aml * dst)588 Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst)
589 {
590     return build_opcode_2arg_dst(0x72 /* AddOp */, arg1, arg2, dst);
591 }
592 
593 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSubtract */
aml_subtract(Aml * arg1,Aml * arg2,Aml * dst)594 Aml *aml_subtract(Aml *arg1, Aml *arg2, Aml *dst)
595 {
596     return build_opcode_2arg_dst(0x74 /* SubtractOp */, arg1, arg2, dst);
597 }
598 
599 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIncrement */
aml_increment(Aml * arg)600 Aml *aml_increment(Aml *arg)
601 {
602     Aml *var = aml_opcode(0x75 /* IncrementOp */);
603     aml_append(var, arg);
604     return var;
605 }
606 
607 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDecrement */
aml_decrement(Aml * arg)608 Aml *aml_decrement(Aml *arg)
609 {
610     Aml *var = aml_opcode(0x76 /* DecrementOp */);
611     aml_append(var, arg);
612     return var;
613 }
614 
615 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIndex */
aml_index(Aml * arg1,Aml * idx)616 Aml *aml_index(Aml *arg1, Aml *idx)
617 {
618     return build_opcode_2arg_dst(0x88 /* IndexOp */, arg1, idx, NULL);
619 }
620 
621 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefNotify */
aml_notify(Aml * arg1,Aml * arg2)622 Aml *aml_notify(Aml *arg1, Aml *arg2)
623 {
624     Aml *var = aml_opcode(0x86 /* NotifyOp */);
625     aml_append(var, arg1);
626     aml_append(var, arg2);
627     return var;
628 }
629 
630 /* helper to call method without argument */
aml_call0(const char * method)631 Aml *aml_call0(const char *method)
632 {
633     Aml *var = aml_alloc();
634     build_append_namestring(var->buf, "%s", method);
635     return var;
636 }
637 
638 /* helper to call method with 1 argument */
aml_call1(const char * method,Aml * arg1)639 Aml *aml_call1(const char *method, Aml *arg1)
640 {
641     Aml *var = aml_alloc();
642     build_append_namestring(var->buf, "%s", method);
643     aml_append(var, arg1);
644     return var;
645 }
646 
647 /* helper to call method with 2 arguments */
aml_call2(const char * method,Aml * arg1,Aml * arg2)648 Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2)
649 {
650     Aml *var = aml_alloc();
651     build_append_namestring(var->buf, "%s", method);
652     aml_append(var, arg1);
653     aml_append(var, arg2);
654     return var;
655 }
656 
657 /* helper to call method with 3 arguments */
aml_call3(const char * method,Aml * arg1,Aml * arg2,Aml * arg3)658 Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3)
659 {
660     Aml *var = aml_alloc();
661     build_append_namestring(var->buf, "%s", method);
662     aml_append(var, arg1);
663     aml_append(var, arg2);
664     aml_append(var, arg3);
665     return var;
666 }
667 
668 /* helper to call method with 4 arguments */
aml_call4(const char * method,Aml * arg1,Aml * arg2,Aml * arg3,Aml * arg4)669 Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4)
670 {
671     Aml *var = aml_alloc();
672     build_append_namestring(var->buf, "%s", method);
673     aml_append(var, arg1);
674     aml_append(var, arg2);
675     aml_append(var, arg3);
676     aml_append(var, arg4);
677     return var;
678 }
679 
680 /* helper to call method with 5 arguments */
aml_call5(const char * method,Aml * arg1,Aml * arg2,Aml * arg3,Aml * arg4,Aml * arg5)681 Aml *aml_call5(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4,
682                Aml *arg5)
683 {
684     Aml *var = aml_alloc();
685     build_append_namestring(var->buf, "%s", method);
686     aml_append(var, arg1);
687     aml_append(var, arg2);
688     aml_append(var, arg3);
689     aml_append(var, arg4);
690     aml_append(var, arg5);
691     return var;
692 }
693 
694 /*
695  * ACPI 5.0: 6.4.3.8.1 GPIO Connection Descriptor
696  * Type 1, Large Item Name 0xC
697  */
698 
aml_gpio_connection(AmlGpioConnectionType type,AmlConsumerAndProducer con_and_pro,uint8_t flags,AmlPinConfig pin_config,uint16_t output_drive,uint16_t debounce_timeout,const uint32_t pin_list[],uint32_t pin_count,const char * resource_source_name,const uint8_t * vendor_data,uint16_t vendor_data_len)699 static Aml *aml_gpio_connection(AmlGpioConnectionType type,
700                                 AmlConsumerAndProducer con_and_pro,
701                                 uint8_t flags, AmlPinConfig pin_config,
702                                 uint16_t output_drive,
703                                 uint16_t debounce_timeout,
704                                 const uint32_t pin_list[], uint32_t pin_count,
705                                 const char *resource_source_name,
706                                 const uint8_t *vendor_data,
707                                 uint16_t vendor_data_len)
708 {
709     Aml *var = aml_alloc();
710     const uint16_t min_desc_len = 0x16;
711     uint16_t resource_source_name_len, length;
712     uint16_t pin_table_offset, resource_source_name_offset, vendor_data_offset;
713     uint32_t i;
714 
715     assert(resource_source_name);
716     resource_source_name_len = strlen(resource_source_name) + 1;
717     length = min_desc_len + resource_source_name_len + vendor_data_len;
718     pin_table_offset = min_desc_len + 1;
719     resource_source_name_offset = pin_table_offset + pin_count * 2;
720     vendor_data_offset = resource_source_name_offset + resource_source_name_len;
721 
722     build_append_byte(var->buf, 0x8C);  /* GPIO Connection Descriptor */
723     build_append_int_noprefix(var->buf, length, 2); /* Length */
724     build_append_byte(var->buf, 1);     /* Revision ID */
725     build_append_byte(var->buf, type);  /* GPIO Connection Type */
726     /* General Flags (2 bytes) */
727     build_append_int_noprefix(var->buf, con_and_pro, 2);
728     /* Interrupt and IO Flags (2 bytes) */
729     build_append_int_noprefix(var->buf, flags, 2);
730     /* Pin Configuration 0 = Default 1 = Pull-up 2 = Pull-down 3 = No Pull */
731     build_append_byte(var->buf, pin_config);
732     /* Output Drive Strength (2 bytes) */
733     build_append_int_noprefix(var->buf, output_drive, 2);
734     /* Debounce Timeout (2 bytes) */
735     build_append_int_noprefix(var->buf, debounce_timeout, 2);
736     /* Pin Table Offset (2 bytes) */
737     build_append_int_noprefix(var->buf, pin_table_offset, 2);
738     build_append_byte(var->buf, 0);     /* Resource Source Index */
739     /* Resource Source Name Offset (2 bytes) */
740     build_append_int_noprefix(var->buf, resource_source_name_offset, 2);
741     /* Vendor Data Offset (2 bytes) */
742     build_append_int_noprefix(var->buf, vendor_data_offset, 2);
743     /* Vendor Data Length (2 bytes) */
744     build_append_int_noprefix(var->buf, vendor_data_len, 2);
745     /* Pin Number (2n bytes)*/
746     for (i = 0; i < pin_count; i++) {
747         build_append_int_noprefix(var->buf, pin_list[i], 2);
748     }
749 
750     /* Resource Source Name */
751     build_append_namestring(var->buf, "%s", resource_source_name);
752     build_append_byte(var->buf, '\0');
753 
754     /* Vendor-defined Data */
755     if (vendor_data != NULL) {
756         g_array_append_vals(var->buf, vendor_data, vendor_data_len);
757     }
758 
759     return var;
760 }
761 
762 /*
763  * ACPI 5.0: 19.5.53
764  * GpioInt(GPIO Interrupt Connection Resource Descriptor Macro)
765  */
aml_gpio_int(AmlConsumerAndProducer con_and_pro,AmlLevelAndEdge edge_level,AmlActiveHighAndLow active_level,AmlShared shared,AmlPinConfig pin_config,uint16_t debounce_timeout,const uint32_t pin_list[],uint32_t pin_count,const char * resource_source_name,const uint8_t * vendor_data,uint16_t vendor_data_len)766 Aml *aml_gpio_int(AmlConsumerAndProducer con_and_pro,
767                   AmlLevelAndEdge edge_level,
768                   AmlActiveHighAndLow active_level, AmlShared shared,
769                   AmlPinConfig pin_config, uint16_t debounce_timeout,
770                   const uint32_t pin_list[], uint32_t pin_count,
771                   const char *resource_source_name,
772                   const uint8_t *vendor_data, uint16_t vendor_data_len)
773 {
774     uint8_t flags = edge_level | (active_level << 1) | (shared << 3);
775 
776     return aml_gpio_connection(AML_INTERRUPT_CONNECTION, con_and_pro, flags,
777                                pin_config, 0, debounce_timeout, pin_list,
778                                pin_count, resource_source_name, vendor_data,
779                                vendor_data_len);
780 }
781 
782 /*
783  * ACPI 1.0b: 6.4.3.4 32-Bit Fixed Location Memory Range Descriptor
784  * (Type 1, Large Item Name 0x6)
785  */
aml_memory32_fixed(uint32_t addr,uint32_t size,AmlReadAndWrite read_and_write)786 Aml *aml_memory32_fixed(uint32_t addr, uint32_t size,
787                         AmlReadAndWrite read_and_write)
788 {
789     Aml *var = aml_alloc();
790     build_append_byte(var->buf, 0x86); /* Memory32Fixed Resource Descriptor */
791     build_append_byte(var->buf, 9);    /* Length, bits[7:0] value = 9 */
792     build_append_byte(var->buf, 0);    /* Length, bits[15:8] value = 0 */
793     build_append_byte(var->buf, read_and_write); /* Write status, 1 rw 0 ro */
794 
795     /* Range base address */
796     build_append_byte(var->buf, extract32(addr, 0, 8));  /* bits[7:0] */
797     build_append_byte(var->buf, extract32(addr, 8, 8));  /* bits[15:8] */
798     build_append_byte(var->buf, extract32(addr, 16, 8)); /* bits[23:16] */
799     build_append_byte(var->buf, extract32(addr, 24, 8)); /* bits[31:24] */
800 
801     /* Range length */
802     build_append_byte(var->buf, extract32(size, 0, 8));  /* bits[7:0] */
803     build_append_byte(var->buf, extract32(size, 8, 8));  /* bits[15:8] */
804     build_append_byte(var->buf, extract32(size, 16, 8)); /* bits[23:16] */
805     build_append_byte(var->buf, extract32(size, 24, 8)); /* bits[31:24] */
806     return var;
807 }
808 
809 /*
810  * ACPI 5.0: 6.4.3.6 Extended Interrupt Descriptor
811  * Type 1, Large Item Name 0x9
812  */
aml_interrupt(AmlConsumerAndProducer con_and_pro,AmlLevelAndEdge level_and_edge,AmlActiveHighAndLow high_and_low,AmlShared shared,uint32_t * irq_list,uint8_t irq_count)813 Aml *aml_interrupt(AmlConsumerAndProducer con_and_pro,
814                    AmlLevelAndEdge level_and_edge,
815                    AmlActiveHighAndLow high_and_low, AmlShared shared,
816                    uint32_t *irq_list, uint8_t irq_count)
817 {
818     int i;
819     Aml *var = aml_alloc();
820     uint8_t irq_flags = con_and_pro | (level_and_edge << 1)
821                         | (high_and_low << 2) | (shared << 3);
822     const int header_bytes_in_len = 2;
823     uint16_t len = header_bytes_in_len + irq_count * sizeof(uint32_t);
824 
825     assert(irq_count > 0);
826 
827     build_append_byte(var->buf, 0x89); /* Extended irq descriptor */
828     build_append_byte(var->buf, len & 0xFF); /* Length, bits[7:0] */
829     build_append_byte(var->buf, len >> 8); /* Length, bits[15:8] */
830     build_append_byte(var->buf, irq_flags); /* Interrupt Vector Information. */
831     build_append_byte(var->buf, irq_count);   /* Interrupt table length */
832 
833     /* Interrupt Number List */
834     for (i = 0; i < irq_count; i++) {
835         build_append_int_noprefix(var->buf, irq_list[i], 4);
836     }
837     return var;
838 }
839 
840 /* ACPI 1.0b: 6.4.2.5 I/O Port Descriptor */
aml_io(AmlIODecode dec,uint16_t min_base,uint16_t max_base,uint8_t aln,uint8_t len)841 Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base,
842             uint8_t aln, uint8_t len)
843 {
844     Aml *var = aml_alloc();
845     build_append_byte(var->buf, 0x47); /* IO port descriptor */
846     build_append_byte(var->buf, dec);
847     build_append_byte(var->buf, min_base & 0xff);
848     build_append_byte(var->buf, (min_base >> 8) & 0xff);
849     build_append_byte(var->buf, max_base & 0xff);
850     build_append_byte(var->buf, (max_base >> 8) & 0xff);
851     build_append_byte(var->buf, aln);
852     build_append_byte(var->buf, len);
853     return var;
854 }
855 
856 /*
857  * ACPI 1.0b: 6.4.2.1.1 ASL Macro for IRQ Descriptor
858  *
859  * More verbose description at:
860  * ACPI 5.0: 19.5.64 IRQNoFlags (Interrupt Resource Descriptor Macro)
861  *           6.4.2.1 IRQ Descriptor
862  */
aml_irq_no_flags(uint8_t irq)863 Aml *aml_irq_no_flags(uint8_t irq)
864 {
865     uint16_t irq_mask;
866     Aml *var = aml_alloc();
867 
868     assert(irq < 16);
869     build_append_byte(var->buf, 0x22); /* IRQ descriptor 2 byte form */
870 
871     irq_mask = 1U << irq;
872     build_append_byte(var->buf, irq_mask & 0xFF); /* IRQ mask bits[7:0] */
873     build_append_byte(var->buf, irq_mask >> 8); /* IRQ mask bits[15:8] */
874     return var;
875 }
876 
877 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLNot */
aml_lnot(Aml * arg)878 Aml *aml_lnot(Aml *arg)
879 {
880     Aml *var = aml_opcode(0x92 /* LNotOp */);
881     aml_append(var, arg);
882     return var;
883 }
884 
885 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLEqual */
aml_equal(Aml * arg1,Aml * arg2)886 Aml *aml_equal(Aml *arg1, Aml *arg2)
887 {
888     Aml *var = aml_opcode(0x93 /* LequalOp */);
889     aml_append(var, arg1);
890     aml_append(var, arg2);
891     return var;
892 }
893 
894 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLGreater */
aml_lgreater(Aml * arg1,Aml * arg2)895 Aml *aml_lgreater(Aml *arg1, Aml *arg2)
896 {
897     Aml *var = aml_opcode(0x94 /* LGreaterOp */);
898     aml_append(var, arg1);
899     aml_append(var, arg2);
900     return var;
901 }
902 
903 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLGreaterEqual */
aml_lgreater_equal(Aml * arg1,Aml * arg2)904 Aml *aml_lgreater_equal(Aml *arg1, Aml *arg2)
905 {
906     /* LGreaterEqualOp := LNotOp LLessOp */
907     Aml *var = aml_opcode(0x92 /* LNotOp */);
908     build_append_byte(var->buf, 0x95 /* LLessOp */);
909     aml_append(var, arg1);
910     aml_append(var, arg2);
911     return var;
912 }
913 
914 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */
aml_if(Aml * predicate)915 Aml *aml_if(Aml *predicate)
916 {
917     Aml *var = aml_bundle(0xA0 /* IfOp */, AML_PACKAGE);
918     aml_append(var, predicate);
919     return var;
920 }
921 
922 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefElse */
aml_else(void)923 Aml *aml_else(void)
924 {
925     Aml *var = aml_bundle(0xA1 /* ElseOp */, AML_PACKAGE);
926     return var;
927 }
928 
929 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefWhile */
aml_while(Aml * predicate)930 Aml *aml_while(Aml *predicate)
931 {
932     Aml *var = aml_bundle(0xA2 /* WhileOp */, AML_PACKAGE);
933     aml_append(var, predicate);
934     return var;
935 }
936 
937 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */
aml_method(const char * name,int arg_count,AmlSerializeFlag sflag)938 Aml *aml_method(const char *name, int arg_count, AmlSerializeFlag sflag)
939 {
940     Aml *var = aml_bundle(0x14 /* MethodOp */, AML_PACKAGE);
941     int methodflags;
942 
943     /*
944      * MethodFlags:
945      *   bit 0-2: ArgCount (0-7)
946      *   bit 3: SerializeFlag
947      *     0: NotSerialized
948      *     1: Serialized
949      *   bit 4-7: reserved (must be 0)
950      */
951     assert(arg_count < 8);
952     methodflags = arg_count | (sflag << 3);
953 
954     build_append_namestring(var->buf, "%s", name);
955     build_append_byte(var->buf, methodflags); /* MethodFlags: ArgCount */
956     return var;
957 }
958 
959 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */
aml_device(const char * name_format,...)960 Aml *aml_device(const char *name_format, ...)
961 {
962     va_list ap;
963     Aml *var = aml_bundle(0x82 /* DeviceOp */, AML_EXT_PACKAGE);
964     va_start(ap, name_format);
965     build_append_namestringv(var->buf, name_format, ap);
966     va_end(ap);
967     return var;
968 }
969 
970 /* ACPI 1.0b: 6.4.1 ASL Macros for Resource Descriptors */
aml_resource_template(void)971 Aml *aml_resource_template(void)
972 {
973     /* ResourceTemplate is a buffer of Resources with EndTag at the end */
974     Aml *var = aml_bundle(0x11 /* BufferOp */, AML_RES_TEMPLATE);
975     return var;
976 }
977 
978 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefBuffer
979  * Pass byte_list as NULL to request uninitialized buffer to reserve space.
980  */
aml_buffer(int buffer_size,uint8_t * byte_list)981 Aml *aml_buffer(int buffer_size, uint8_t *byte_list)
982 {
983     int i;
984     Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER);
985 
986     for (i = 0; i < buffer_size; i++) {
987         if (byte_list == NULL) {
988             build_append_byte(var->buf, 0x0);
989         } else {
990             build_append_byte(var->buf, byte_list[i]);
991         }
992     }
993 
994     return var;
995 }
996 
997 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefPackage */
aml_package(uint8_t num_elements)998 Aml *aml_package(uint8_t num_elements)
999 {
1000     Aml *var = aml_bundle(0x12 /* PackageOp */, AML_PACKAGE);
1001     build_append_byte(var->buf, num_elements);
1002     return var;
1003 }
1004 
1005 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefOpRegion */
aml_operation_region(const char * name,AmlRegionSpace rs,Aml * offset,uint32_t len)1006 Aml *aml_operation_region(const char *name, AmlRegionSpace rs,
1007                           Aml *offset, uint32_t len)
1008 {
1009     Aml *var = aml_alloc();
1010     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1011     build_append_byte(var->buf, 0x80); /* OpRegionOp */
1012     build_append_namestring(var->buf, "%s", name);
1013     build_append_byte(var->buf, rs);
1014     aml_append(var, offset);
1015     build_append_int(var->buf, len);
1016     return var;
1017 }
1018 
1019 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: NamedField */
aml_named_field(const char * name,unsigned length)1020 Aml *aml_named_field(const char *name, unsigned length)
1021 {
1022     Aml *var = aml_alloc();
1023     build_append_nameseg(var->buf, name);
1024     build_append_pkg_length(var->buf, length, false);
1025     return var;
1026 }
1027 
1028 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: ReservedField */
aml_reserved_field(unsigned length)1029 Aml *aml_reserved_field(unsigned length)
1030 {
1031     Aml *var = aml_alloc();
1032     /* ReservedField  := 0x00 PkgLength */
1033     build_append_byte(var->buf, 0x00);
1034     build_append_pkg_length(var->buf, length, false);
1035     return var;
1036 }
1037 
1038 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefField */
aml_field(const char * name,AmlAccessType type,AmlLockRule lock,AmlUpdateRule rule)1039 Aml *aml_field(const char *name, AmlAccessType type, AmlLockRule lock,
1040                AmlUpdateRule rule)
1041 {
1042     Aml *var = aml_bundle(0x81 /* FieldOp */, AML_EXT_PACKAGE);
1043     uint8_t flags = rule << 5 | type;
1044 
1045     flags |= lock << 4; /* LockRule at 4 bit offset */
1046 
1047     build_append_namestring(var->buf, "%s", name);
1048     build_append_byte(var->buf, flags);
1049     return var;
1050 }
1051 
1052 static
create_field_common(int opcode,Aml * srcbuf,Aml * index,const char * name)1053 Aml *create_field_common(int opcode, Aml *srcbuf, Aml *index, const char *name)
1054 {
1055     Aml *var = aml_opcode(opcode);
1056     aml_append(var, srcbuf);
1057     aml_append(var, index);
1058     build_append_namestring(var->buf, "%s", name);
1059     return var;
1060 }
1061 
1062 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefCreateField */
aml_create_field(Aml * srcbuf,Aml * bit_index,Aml * num_bits,const char * name)1063 Aml *aml_create_field(Aml *srcbuf, Aml *bit_index, Aml *num_bits,
1064                       const char *name)
1065 {
1066     Aml *var = aml_alloc();
1067     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1068     build_append_byte(var->buf, 0x13); /* CreateFieldOp */
1069     aml_append(var, srcbuf);
1070     aml_append(var, bit_index);
1071     aml_append(var, num_bits);
1072     build_append_namestring(var->buf, "%s", name);
1073     return var;
1074 }
1075 
1076 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefCreateDWordField */
aml_create_dword_field(Aml * srcbuf,Aml * index,const char * name)1077 Aml *aml_create_dword_field(Aml *srcbuf, Aml *index, const char *name)
1078 {
1079     return create_field_common(0x8A /* CreateDWordFieldOp */,
1080                                srcbuf, index, name);
1081 }
1082 
1083 /* ACPI 2.0a: 17.2.4.2 Named Objects Encoding: DefCreateQWordField */
aml_create_qword_field(Aml * srcbuf,Aml * index,const char * name)1084 Aml *aml_create_qword_field(Aml *srcbuf, Aml *index, const char *name)
1085 {
1086     return create_field_common(0x8F /* CreateQWordFieldOp */,
1087                                srcbuf, index, name);
1088 }
1089 
1090 /* ACPI 1.0b: 16.2.3 Data Objects Encoding: String */
aml_string(const char * name_format,...)1091 Aml *aml_string(const char *name_format, ...)
1092 {
1093     Aml *var = aml_opcode(0x0D /* StringPrefix */);
1094     va_list ap;
1095     char *s;
1096     int len;
1097 
1098     va_start(ap, name_format);
1099     len = g_vasprintf(&s, name_format, ap);
1100     va_end(ap);
1101 
1102     g_array_append_vals(var->buf, s, len + 1);
1103     g_free(s);
1104 
1105     return var;
1106 }
1107 
1108 /* ACPI 1.0b: 16.2.6.2 Local Objects Encoding */
aml_local(int num)1109 Aml *aml_local(int num)
1110 {
1111     uint8_t op = 0x60 /* Local0Op */ + num;
1112 
1113     assert(num <= 7);
1114     return aml_opcode(op);
1115 }
1116 
1117 /* ACPI 2.0a: 17.2.2 Data Objects Encoding: DefVarPackage */
aml_varpackage(uint32_t num_elements)1118 Aml *aml_varpackage(uint32_t num_elements)
1119 {
1120     Aml *var = aml_bundle(0x13 /* VarPackageOp */, AML_PACKAGE);
1121     build_append_int(var->buf, num_elements);
1122     return var;
1123 }
1124 
1125 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefProcessor */
aml_processor(uint8_t proc_id,uint32_t pblk_addr,uint8_t pblk_len,const char * name_format,...)1126 Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len,
1127                    const char *name_format, ...)
1128 {
1129     va_list ap;
1130     Aml *var = aml_bundle(0x83 /* ProcessorOp */, AML_EXT_PACKAGE);
1131     va_start(ap, name_format);
1132     build_append_namestringv(var->buf, name_format, ap);
1133     va_end(ap);
1134     build_append_byte(var->buf, proc_id); /* ProcID */
1135     build_append_int_noprefix(var->buf, pblk_addr, sizeof(pblk_addr));
1136     build_append_byte(var->buf, pblk_len); /* PblkLen */
1137     return var;
1138 }
1139 
Hex2Digit(char c)1140 static uint8_t Hex2Digit(char c)
1141 {
1142     if (c >= 'A') {
1143         return c - 'A' + 10;
1144     }
1145 
1146     return c - '0';
1147 }
1148 
1149 /* ACPI 1.0b: 15.2.3.6.4.1 EISAID Macro - Convert EISA ID String To Integer */
aml_eisaid(const char * str)1150 Aml *aml_eisaid(const char *str)
1151 {
1152     Aml *var = aml_alloc();
1153     uint32_t id;
1154 
1155     g_assert(strlen(str) == 7);
1156     id = (str[0] - 0x40) << 26 |
1157     (str[1] - 0x40) << 21 |
1158     (str[2] - 0x40) << 16 |
1159     Hex2Digit(str[3]) << 12 |
1160     Hex2Digit(str[4]) << 8 |
1161     Hex2Digit(str[5]) << 4 |
1162     Hex2Digit(str[6]);
1163 
1164     build_append_byte(var->buf, 0x0C); /* DWordPrefix */
1165     build_append_int_noprefix(var->buf, bswap32(id), sizeof(id));
1166     return var;
1167 }
1168 
1169 /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor: bytes 3-5 */
aml_as_desc_header(AmlResourceType type,AmlMinFixed min_fixed,AmlMaxFixed max_fixed,AmlDecode dec,uint8_t type_flags)1170 static Aml *aml_as_desc_header(AmlResourceType type, AmlMinFixed min_fixed,
1171                                AmlMaxFixed max_fixed, AmlDecode dec,
1172                                uint8_t type_flags)
1173 {
1174     uint8_t flags = max_fixed | min_fixed | dec;
1175     Aml *var = aml_alloc();
1176 
1177     build_append_byte(var->buf, type);
1178     build_append_byte(var->buf, flags);
1179     build_append_byte(var->buf, type_flags); /* Type Specific Flags */
1180     return var;
1181 }
1182 
1183 /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor */
aml_word_as_desc(AmlResourceType type,AmlMinFixed min_fixed,AmlMaxFixed max_fixed,AmlDecode dec,uint16_t addr_gran,uint16_t addr_min,uint16_t addr_max,uint16_t addr_trans,uint16_t len,uint8_t type_flags)1184 static Aml *aml_word_as_desc(AmlResourceType type, AmlMinFixed min_fixed,
1185                              AmlMaxFixed max_fixed, AmlDecode dec,
1186                              uint16_t addr_gran, uint16_t addr_min,
1187                              uint16_t addr_max, uint16_t addr_trans,
1188                              uint16_t len, uint8_t type_flags)
1189 {
1190     Aml *var = aml_alloc();
1191 
1192     build_append_byte(var->buf, 0x88); /* Word Address Space Descriptor */
1193     /* minimum length since we do not encode optional fields */
1194     build_append_byte(var->buf, 0x0D);
1195     build_append_byte(var->buf, 0x0);
1196 
1197     aml_append(var,
1198         aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags));
1199     build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran));
1200     build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min));
1201     build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max));
1202     build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans));
1203     build_append_int_noprefix(var->buf, len, sizeof(len));
1204     return var;
1205 }
1206 
1207 /* ACPI 1.0b: 6.4.3.5.3 DWord Address Space Descriptor */
aml_dword_as_desc(AmlResourceType type,AmlMinFixed min_fixed,AmlMaxFixed max_fixed,AmlDecode dec,uint32_t addr_gran,uint32_t addr_min,uint32_t addr_max,uint32_t addr_trans,uint32_t len,uint8_t type_flags)1208 static Aml *aml_dword_as_desc(AmlResourceType type, AmlMinFixed min_fixed,
1209                               AmlMaxFixed max_fixed, AmlDecode dec,
1210                               uint32_t addr_gran, uint32_t addr_min,
1211                               uint32_t addr_max, uint32_t addr_trans,
1212                               uint32_t len, uint8_t type_flags)
1213 {
1214     Aml *var = aml_alloc();
1215 
1216     build_append_byte(var->buf, 0x87); /* DWord Address Space Descriptor */
1217     /* minimum length since we do not encode optional fields */
1218     build_append_byte(var->buf, 23);
1219     build_append_byte(var->buf, 0x0);
1220 
1221 
1222     aml_append(var,
1223         aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags));
1224     build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran));
1225     build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min));
1226     build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max));
1227     build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans));
1228     build_append_int_noprefix(var->buf, len, sizeof(len));
1229     return var;
1230 }
1231 
1232 /* ACPI 1.0b: 6.4.3.5.1 QWord Address Space Descriptor */
aml_qword_as_desc(AmlResourceType type,AmlMinFixed min_fixed,AmlMaxFixed max_fixed,AmlDecode dec,uint64_t addr_gran,uint64_t addr_min,uint64_t addr_max,uint64_t addr_trans,uint64_t len,uint8_t type_flags)1233 static Aml *aml_qword_as_desc(AmlResourceType type, AmlMinFixed min_fixed,
1234                               AmlMaxFixed max_fixed, AmlDecode dec,
1235                               uint64_t addr_gran, uint64_t addr_min,
1236                               uint64_t addr_max, uint64_t addr_trans,
1237                               uint64_t len, uint8_t type_flags)
1238 {
1239     Aml *var = aml_alloc();
1240 
1241     build_append_byte(var->buf, 0x8A); /* QWord Address Space Descriptor */
1242     /* minimum length since we do not encode optional fields */
1243     build_append_byte(var->buf, 0x2B);
1244     build_append_byte(var->buf, 0x0);
1245 
1246     aml_append(var,
1247         aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags));
1248     build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran));
1249     build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min));
1250     build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max));
1251     build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans));
1252     build_append_int_noprefix(var->buf, len, sizeof(len));
1253     return var;
1254 }
1255 
1256 /*
1257  * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor
1258  *
1259  * More verbose description at:
1260  * ACPI 5.0: 19.5.141 WordBusNumber (Word Bus Number Resource Descriptor Macro)
1261  */
aml_word_bus_number(AmlMinFixed min_fixed,AmlMaxFixed max_fixed,AmlDecode dec,uint16_t addr_gran,uint16_t addr_min,uint16_t addr_max,uint16_t addr_trans,uint16_t len)1262 Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
1263                          AmlDecode dec, uint16_t addr_gran,
1264                          uint16_t addr_min, uint16_t addr_max,
1265                          uint16_t addr_trans, uint16_t len)
1266 
1267 {
1268     return aml_word_as_desc(AML_BUS_NUMBER_RANGE, min_fixed, max_fixed, dec,
1269                             addr_gran, addr_min, addr_max, addr_trans, len, 0);
1270 }
1271 
1272 /*
1273  * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor
1274  *
1275  * More verbose description at:
1276  * ACPI 5.0: 19.5.142 WordIO (Word IO Resource Descriptor Macro)
1277  */
aml_word_io(AmlMinFixed min_fixed,AmlMaxFixed max_fixed,AmlDecode dec,AmlISARanges isa_ranges,uint16_t addr_gran,uint16_t addr_min,uint16_t addr_max,uint16_t addr_trans,uint16_t len)1278 Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
1279                  AmlDecode dec, AmlISARanges isa_ranges,
1280                  uint16_t addr_gran, uint16_t addr_min,
1281                  uint16_t addr_max, uint16_t addr_trans,
1282                  uint16_t len)
1283 
1284 {
1285     return aml_word_as_desc(AML_IO_RANGE, min_fixed, max_fixed, dec,
1286                             addr_gran, addr_min, addr_max, addr_trans, len,
1287                             isa_ranges);
1288 }
1289 
1290 /*
1291  * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Descriptor
1292  *
1293  * More verbose description at:
1294  * ACPI 5.0: 19.5.33 DWordIO (DWord IO Resource Descriptor Macro)
1295  */
aml_dword_io(AmlMinFixed min_fixed,AmlMaxFixed max_fixed,AmlDecode dec,AmlISARanges isa_ranges,uint32_t addr_gran,uint32_t addr_min,uint32_t addr_max,uint32_t addr_trans,uint32_t len)1296 Aml *aml_dword_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
1297                  AmlDecode dec, AmlISARanges isa_ranges,
1298                  uint32_t addr_gran, uint32_t addr_min,
1299                  uint32_t addr_max, uint32_t addr_trans,
1300                  uint32_t len)
1301 
1302 {
1303     return aml_dword_as_desc(AML_IO_RANGE, min_fixed, max_fixed, dec,
1304                             addr_gran, addr_min, addr_max, addr_trans, len,
1305                             isa_ranges);
1306 }
1307 
1308 /*
1309  * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Space Descriptor
1310  *
1311  * More verbose description at:
1312  * ACPI 5.0: 19.5.34 DWordMemory (DWord Memory Resource Descriptor Macro)
1313  */
aml_dword_memory(AmlDecode dec,AmlMinFixed min_fixed,AmlMaxFixed max_fixed,AmlCacheable cacheable,AmlReadAndWrite read_and_write,uint32_t addr_gran,uint32_t addr_min,uint32_t addr_max,uint32_t addr_trans,uint32_t len)1314 Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed,
1315                       AmlMaxFixed max_fixed, AmlCacheable cacheable,
1316                       AmlReadAndWrite read_and_write,
1317                       uint32_t addr_gran, uint32_t addr_min,
1318                       uint32_t addr_max, uint32_t addr_trans,
1319                       uint32_t len)
1320 {
1321     uint8_t flags = read_and_write | (cacheable << 1);
1322 
1323     return aml_dword_as_desc(AML_MEMORY_RANGE, min_fixed, max_fixed,
1324                              dec, addr_gran, addr_min, addr_max,
1325                              addr_trans, len, flags);
1326 }
1327 
1328 /*
1329  * ACPI 1.0b: 6.4.3.5.2 ASL Macros for QWORD Address Space Descriptor
1330  *
1331  * More verbose description at:
1332  * ACPI 5.0: 19.5.102 QWordMemory (QWord Memory Resource Descriptor Macro)
1333  */
aml_qword_memory(AmlDecode dec,AmlMinFixed min_fixed,AmlMaxFixed max_fixed,AmlCacheable cacheable,AmlReadAndWrite read_and_write,uint64_t addr_gran,uint64_t addr_min,uint64_t addr_max,uint64_t addr_trans,uint64_t len)1334 Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed,
1335                       AmlMaxFixed max_fixed, AmlCacheable cacheable,
1336                       AmlReadAndWrite read_and_write,
1337                       uint64_t addr_gran, uint64_t addr_min,
1338                       uint64_t addr_max, uint64_t addr_trans,
1339                       uint64_t len)
1340 {
1341     uint8_t flags = read_and_write | (cacheable << 1);
1342 
1343     return aml_qword_as_desc(AML_MEMORY_RANGE, min_fixed, max_fixed,
1344                              dec, addr_gran, addr_min, addr_max,
1345                              addr_trans, len, flags);
1346 }
1347 
1348 /* ACPI 1.0b: 6.4.2.2 DMA Format/6.4.2.2.1 ASL Macro for DMA Descriptor */
aml_dma(AmlDmaType typ,AmlDmaBusMaster bm,AmlTransferSize sz,uint8_t channel)1349 Aml *aml_dma(AmlDmaType typ, AmlDmaBusMaster bm, AmlTransferSize sz,
1350              uint8_t channel)
1351 {
1352     Aml *var = aml_alloc();
1353     uint8_t flags = sz | bm << 2 | typ << 5;
1354 
1355     assert(channel < 8);
1356     build_append_byte(var->buf, 0x2A);    /* Byte 0: DMA Descriptor */
1357     build_append_byte(var->buf, 1U << channel); /* Byte 1: _DMA - DmaChannel */
1358     build_append_byte(var->buf, flags);   /* Byte 2 */
1359     return var;
1360 }
1361 
1362 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefSleep */
aml_sleep(uint64_t msec)1363 Aml *aml_sleep(uint64_t msec)
1364 {
1365     Aml *var = aml_alloc();
1366     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1367     build_append_byte(var->buf, 0x22); /* SleepOp */
1368     aml_append(var, aml_int(msec));
1369     return var;
1370 }
1371 
Hex2Byte(const char * src)1372 static uint8_t Hex2Byte(const char *src)
1373 {
1374     int hi, lo;
1375 
1376     hi = Hex2Digit(src[0]);
1377     assert(hi >= 0);
1378     assert(hi <= 15);
1379 
1380     lo = Hex2Digit(src[1]);
1381     assert(lo >= 0);
1382     assert(lo <= 15);
1383     return (hi << 4) | lo;
1384 }
1385 
1386 /*
1387  * ACPI 3.0: 17.5.124 ToUUID (Convert String to UUID Macro)
1388  * e.g. UUID: aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1389  * call aml_touuid("aabbccdd-eeff-gghh-iijj-kkllmmnnoopp");
1390  */
aml_touuid(const char * uuid)1391 Aml *aml_touuid(const char *uuid)
1392 {
1393     Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER);
1394 
1395     assert(strlen(uuid) == 36);
1396     assert(uuid[8] == '-');
1397     assert(uuid[13] == '-');
1398     assert(uuid[18] == '-');
1399     assert(uuid[23] == '-');
1400 
1401     build_append_byte(var->buf, Hex2Byte(uuid + 6));  /* dd - at offset 00 */
1402     build_append_byte(var->buf, Hex2Byte(uuid + 4));  /* cc - at offset 01 */
1403     build_append_byte(var->buf, Hex2Byte(uuid + 2));  /* bb - at offset 02 */
1404     build_append_byte(var->buf, Hex2Byte(uuid + 0));  /* aa - at offset 03 */
1405 
1406     build_append_byte(var->buf, Hex2Byte(uuid + 11)); /* ff - at offset 04 */
1407     build_append_byte(var->buf, Hex2Byte(uuid + 9));  /* ee - at offset 05 */
1408 
1409     build_append_byte(var->buf, Hex2Byte(uuid + 16)); /* hh - at offset 06 */
1410     build_append_byte(var->buf, Hex2Byte(uuid + 14)); /* gg - at offset 07 */
1411 
1412     build_append_byte(var->buf, Hex2Byte(uuid + 19)); /* ii - at offset 08 */
1413     build_append_byte(var->buf, Hex2Byte(uuid + 21)); /* jj - at offset 09 */
1414 
1415     build_append_byte(var->buf, Hex2Byte(uuid + 24)); /* kk - at offset 10 */
1416     build_append_byte(var->buf, Hex2Byte(uuid + 26)); /* ll - at offset 11 */
1417     build_append_byte(var->buf, Hex2Byte(uuid + 28)); /* mm - at offset 12 */
1418     build_append_byte(var->buf, Hex2Byte(uuid + 30)); /* nn - at offset 13 */
1419     build_append_byte(var->buf, Hex2Byte(uuid + 32)); /* oo - at offset 14 */
1420     build_append_byte(var->buf, Hex2Byte(uuid + 34)); /* pp - at offset 15 */
1421 
1422     return var;
1423 }
1424 
1425 /*
1426  * ACPI 2.0b: 16.2.3.6.4.3  Unicode Macro (Convert Ascii String To Unicode)
1427  */
aml_unicode(const char * str)1428 Aml *aml_unicode(const char *str)
1429 {
1430     int i = 0;
1431     Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER);
1432 
1433     do {
1434         build_append_byte(var->buf, str[i]);
1435         build_append_byte(var->buf, 0);
1436         i++;
1437     } while (i <= strlen(str));
1438 
1439     return var;
1440 }
1441 
1442 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefRefOf */
aml_refof(Aml * arg)1443 Aml *aml_refof(Aml *arg)
1444 {
1445     Aml *var = aml_opcode(0x71 /* RefOfOp */);
1446     aml_append(var, arg);
1447     return var;
1448 }
1449 
1450 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDerefOf */
aml_derefof(Aml * arg)1451 Aml *aml_derefof(Aml *arg)
1452 {
1453     Aml *var = aml_opcode(0x83 /* DerefOfOp */);
1454     aml_append(var, arg);
1455     return var;
1456 }
1457 
1458 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSizeOf */
aml_sizeof(Aml * arg)1459 Aml *aml_sizeof(Aml *arg)
1460 {
1461     Aml *var = aml_opcode(0x87 /* SizeOfOp */);
1462     aml_append(var, arg);
1463     return var;
1464 }
1465 
1466 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMutex */
aml_mutex(const char * name,uint8_t sync_level)1467 Aml *aml_mutex(const char *name, uint8_t sync_level)
1468 {
1469     Aml *var = aml_alloc();
1470     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1471     build_append_byte(var->buf, 0x01); /* MutexOp */
1472     build_append_namestring(var->buf, "%s", name);
1473     assert(!(sync_level & 0xF0));
1474     build_append_byte(var->buf, sync_level);
1475     return var;
1476 }
1477 
1478 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAcquire */
aml_acquire(Aml * mutex,uint16_t timeout)1479 Aml *aml_acquire(Aml *mutex, uint16_t timeout)
1480 {
1481     Aml *var = aml_alloc();
1482     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1483     build_append_byte(var->buf, 0x23); /* AcquireOp */
1484     aml_append(var, mutex);
1485     build_append_int_noprefix(var->buf, timeout, sizeof(timeout));
1486     return var;
1487 }
1488 
1489 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefRelease */
aml_release(Aml * mutex)1490 Aml *aml_release(Aml *mutex)
1491 {
1492     Aml *var = aml_alloc();
1493     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1494     build_append_byte(var->buf, 0x27); /* ReleaseOp */
1495     aml_append(var, mutex);
1496     return var;
1497 }
1498 
1499 /* ACPI 1.0b: 16.2.5.1 Name Space Modifier Objects Encoding: DefAlias */
aml_alias(const char * source_object,const char * alias_object)1500 Aml *aml_alias(const char *source_object, const char *alias_object)
1501 {
1502     Aml *var = aml_opcode(0x06 /* AliasOp */);
1503     aml_append(var, aml_name("%s", source_object));
1504     aml_append(var, aml_name("%s", alias_object));
1505     return var;
1506 }
1507 
1508 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefConcat */
aml_concatenate(Aml * source1,Aml * source2,Aml * target)1509 Aml *aml_concatenate(Aml *source1, Aml *source2, Aml *target)
1510 {
1511     return build_opcode_2arg_dst(0x73 /* ConcatOp */, source1, source2,
1512                                  target);
1513 }
1514 
1515 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefObjectType */
aml_object_type(Aml * object)1516 Aml *aml_object_type(Aml *object)
1517 {
1518     Aml *var = aml_opcode(0x8E /* ObjectTypeOp */);
1519     aml_append(var, object);
1520     return var;
1521 }
1522 
1523 void
build_header(BIOSLinker * linker,GArray * table_data,AcpiTableHeader * h,const char * sig,int len,uint8_t rev,const char * oem_id,const char * oem_table_id)1524 build_header(BIOSLinker *linker, GArray *table_data,
1525              AcpiTableHeader *h, const char *sig, int len, uint8_t rev,
1526              const char *oem_id, const char *oem_table_id)
1527 {
1528     unsigned tbl_offset = (char *)h - table_data->data;
1529     unsigned checksum_offset = (char *)&h->checksum - table_data->data;
1530     memcpy(&h->signature, sig, 4);
1531     h->length = cpu_to_le32(len);
1532     h->revision = rev;
1533 
1534     if (oem_id) {
1535         strncpy((char *)h->oem_id, oem_id, sizeof h->oem_id);
1536     } else {
1537         memcpy(h->oem_id, ACPI_BUILD_APPNAME6, 6);
1538     }
1539 
1540     if (oem_table_id) {
1541         strncpy((char *)h->oem_table_id, oem_table_id, sizeof(h->oem_table_id));
1542     } else {
1543         memcpy(h->oem_table_id, ACPI_BUILD_APPNAME4, 4);
1544         memcpy(h->oem_table_id + 4, sig, 4);
1545     }
1546 
1547     h->oem_revision = cpu_to_le32(1);
1548     memcpy(h->asl_compiler_id, ACPI_BUILD_APPNAME4, 4);
1549     h->asl_compiler_revision = cpu_to_le32(1);
1550     /* Checksum to be filled in by Guest linker */
1551     bios_linker_loader_add_checksum(linker, ACPI_BUILD_TABLE_FILE,
1552         tbl_offset, len, checksum_offset);
1553 }
1554 
acpi_data_push(GArray * table_data,unsigned size)1555 void *acpi_data_push(GArray *table_data, unsigned size)
1556 {
1557     unsigned off = table_data->len;
1558     g_array_set_size(table_data, off + size);
1559     return table_data->data + off;
1560 }
1561 
acpi_data_len(GArray * table)1562 unsigned acpi_data_len(GArray *table)
1563 {
1564     assert(g_array_get_element_size(table) == 1);
1565     return table->len;
1566 }
1567 
acpi_add_table(GArray * table_offsets,GArray * table_data)1568 void acpi_add_table(GArray *table_offsets, GArray *table_data)
1569 {
1570     uint32_t offset = table_data->len;
1571     g_array_append_val(table_offsets, offset);
1572 }
1573 
acpi_build_tables_init(AcpiBuildTables * tables)1574 void acpi_build_tables_init(AcpiBuildTables *tables)
1575 {
1576     tables->rsdp = g_array_new(false, true /* clear */, 1);
1577     tables->table_data = g_array_new(false, true /* clear */, 1);
1578     tables->tcpalog = g_array_new(false, true /* clear */, 1);
1579     tables->vmgenid = g_array_new(false, true /* clear */, 1);
1580     tables->linker = bios_linker_loader_init();
1581 }
1582 
acpi_build_tables_cleanup(AcpiBuildTables * tables,bool mfre)1583 void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre)
1584 {
1585     bios_linker_loader_cleanup(tables->linker);
1586     g_array_free(tables->rsdp, true);
1587     g_array_free(tables->table_data, true);
1588     g_array_free(tables->tcpalog, mfre);
1589     g_array_free(tables->vmgenid, mfre);
1590 }
1591 
1592 /* Build rsdt table */
1593 void
build_rsdt(GArray * table_data,BIOSLinker * linker,GArray * table_offsets,const char * oem_id,const char * oem_table_id)1594 build_rsdt(GArray *table_data, BIOSLinker *linker, GArray *table_offsets,
1595            const char *oem_id, const char *oem_table_id)
1596 {
1597     int i;
1598     unsigned rsdt_entries_offset;
1599     AcpiRsdtDescriptorRev1 *rsdt;
1600     const unsigned table_data_len = (sizeof(uint32_t) * table_offsets->len);
1601     const unsigned rsdt_entry_size = sizeof(rsdt->table_offset_entry[0]);
1602     const size_t rsdt_len = sizeof(*rsdt) + table_data_len;
1603 
1604     rsdt = acpi_data_push(table_data, rsdt_len);
1605     rsdt_entries_offset = (char *)rsdt->table_offset_entry - table_data->data;
1606     for (i = 0; i < table_offsets->len; ++i) {
1607         uint32_t ref_tbl_offset = g_array_index(table_offsets, uint32_t, i);
1608         uint32_t rsdt_entry_offset = rsdt_entries_offset + rsdt_entry_size * i;
1609 
1610         /* rsdt->table_offset_entry to be filled by Guest linker */
1611         bios_linker_loader_add_pointer(linker,
1612             ACPI_BUILD_TABLE_FILE, rsdt_entry_offset, rsdt_entry_size,
1613             ACPI_BUILD_TABLE_FILE, ref_tbl_offset);
1614     }
1615     build_header(linker, table_data,
1616                  (void *)rsdt, "RSDT", rsdt_len, 1, oem_id, oem_table_id);
1617 }
1618 
1619 /* Build xsdt table */
1620 void
build_xsdt(GArray * table_data,BIOSLinker * linker,GArray * table_offsets,const char * oem_id,const char * oem_table_id)1621 build_xsdt(GArray *table_data, BIOSLinker *linker, GArray *table_offsets,
1622            const char *oem_id, const char *oem_table_id)
1623 {
1624     int i;
1625     unsigned xsdt_entries_offset;
1626     AcpiXsdtDescriptorRev2 *xsdt;
1627     const unsigned table_data_len = (sizeof(uint64_t) * table_offsets->len);
1628     const unsigned xsdt_entry_size = sizeof(xsdt->table_offset_entry[0]);
1629     const size_t xsdt_len = sizeof(*xsdt) + table_data_len;
1630 
1631     xsdt = acpi_data_push(table_data, xsdt_len);
1632     xsdt_entries_offset = (char *)xsdt->table_offset_entry - table_data->data;
1633     for (i = 0; i < table_offsets->len; ++i) {
1634         uint64_t ref_tbl_offset = g_array_index(table_offsets, uint32_t, i);
1635         uint64_t xsdt_entry_offset = xsdt_entries_offset + xsdt_entry_size * i;
1636 
1637         /* xsdt->table_offset_entry to be filled by Guest linker */
1638         bios_linker_loader_add_pointer(linker,
1639             ACPI_BUILD_TABLE_FILE, xsdt_entry_offset, xsdt_entry_size,
1640             ACPI_BUILD_TABLE_FILE, ref_tbl_offset);
1641     }
1642     build_header(linker, table_data,
1643                  (void *)xsdt, "XSDT", xsdt_len, 1, oem_id, oem_table_id);
1644 }
1645 
build_srat_memory(AcpiSratMemoryAffinity * numamem,uint64_t base,uint64_t len,int node,MemoryAffinityFlags flags)1646 void build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base,
1647                        uint64_t len, int node, MemoryAffinityFlags flags)
1648 {
1649     numamem->type = ACPI_SRAT_MEMORY;
1650     numamem->length = sizeof(*numamem);
1651     numamem->proximity = cpu_to_le32(node);
1652     numamem->flags = cpu_to_le32(flags);
1653     numamem->base_addr = cpu_to_le64(base);
1654     numamem->range_length = cpu_to_le64(len);
1655 }
1656 
1657 /*
1658  * ACPI spec 5.2.17 System Locality Distance Information Table
1659  * (Revision 2.0 or later)
1660  */
build_slit(GArray * table_data,BIOSLinker * linker)1661 void build_slit(GArray *table_data, BIOSLinker *linker)
1662 {
1663     int slit_start, i, j;
1664     slit_start = table_data->len;
1665 
1666     acpi_data_push(table_data, sizeof(AcpiTableHeader));
1667 
1668     build_append_int_noprefix(table_data, nb_numa_nodes, 8);
1669     for (i = 0; i < nb_numa_nodes; i++) {
1670         for (j = 0; j < nb_numa_nodes; j++) {
1671             assert(numa_info[i].distance[j]);
1672             build_append_int_noprefix(table_data, numa_info[i].distance[j], 1);
1673         }
1674     }
1675 
1676     build_header(linker, table_data,
1677                  (void *)(table_data->data + slit_start),
1678                  "SLIT",
1679                  table_data->len - slit_start, 1, NULL, NULL);
1680 }
1681 
1682 /* build rev1/rev3/rev5.1 FADT */
build_fadt(GArray * tbl,BIOSLinker * linker,const AcpiFadtData * f,const char * oem_id,const char * oem_table_id)1683 void build_fadt(GArray *tbl, BIOSLinker *linker, const AcpiFadtData *f,
1684                 const char *oem_id, const char *oem_table_id)
1685 {
1686     int off;
1687     int fadt_start = tbl->len;
1688 
1689     acpi_data_push(tbl, sizeof(AcpiTableHeader));
1690 
1691     /* FACS address to be filled by Guest linker at runtime */
1692     off = tbl->len;
1693     build_append_int_noprefix(tbl, 0, 4); /* FIRMWARE_CTRL */
1694     if (f->facs_tbl_offset) { /* don't patch if not supported by platform */
1695         bios_linker_loader_add_pointer(linker,
1696             ACPI_BUILD_TABLE_FILE, off, 4,
1697             ACPI_BUILD_TABLE_FILE, *f->facs_tbl_offset);
1698     }
1699 
1700     /* DSDT address to be filled by Guest linker at runtime */
1701     off = tbl->len;
1702     build_append_int_noprefix(tbl, 0, 4); /* DSDT */
1703     if (f->dsdt_tbl_offset) { /* don't patch if not supported by platform */
1704         bios_linker_loader_add_pointer(linker,
1705             ACPI_BUILD_TABLE_FILE, off, 4,
1706             ACPI_BUILD_TABLE_FILE, *f->dsdt_tbl_offset);
1707     }
1708 
1709     /* ACPI1.0: INT_MODEL, ACPI2.0+: Reserved */
1710     build_append_int_noprefix(tbl, f->int_model /* Multiple APIC */, 1);
1711     /* Preferred_PM_Profile */
1712     build_append_int_noprefix(tbl, 0 /* Unspecified */, 1);
1713     build_append_int_noprefix(tbl, f->sci_int, 2); /* SCI_INT */
1714     build_append_int_noprefix(tbl, f->smi_cmd, 4); /* SMI_CMD */
1715     build_append_int_noprefix(tbl, f->acpi_enable_cmd, 1); /* ACPI_ENABLE */
1716     build_append_int_noprefix(tbl, f->acpi_disable_cmd, 1); /* ACPI_DISABLE */
1717     build_append_int_noprefix(tbl, 0 /* not supported */, 1); /* S4BIOS_REQ */
1718     /* ACPI1.0: Reserved, ACPI2.0+: PSTATE_CNT */
1719     build_append_int_noprefix(tbl, 0, 1);
1720     build_append_int_noprefix(tbl, f->pm1a_evt.address, 4); /* PM1a_EVT_BLK */
1721     build_append_int_noprefix(tbl, 0, 4); /* PM1b_EVT_BLK */
1722     build_append_int_noprefix(tbl, f->pm1a_cnt.address, 4); /* PM1a_CNT_BLK */
1723     build_append_int_noprefix(tbl, 0, 4); /* PM1b_CNT_BLK */
1724     build_append_int_noprefix(tbl, 0, 4); /* PM2_CNT_BLK */
1725     build_append_int_noprefix(tbl, f->pm_tmr.address, 4); /* PM_TMR_BLK */
1726     build_append_int_noprefix(tbl, f->gpe0_blk.address, 4); /* GPE0_BLK */
1727     build_append_int_noprefix(tbl, 0, 4); /* GPE1_BLK */
1728     /* PM1_EVT_LEN */
1729     build_append_int_noprefix(tbl, f->pm1a_evt.bit_width / 8, 1);
1730     /* PM1_CNT_LEN */
1731     build_append_int_noprefix(tbl, f->pm1a_cnt.bit_width / 8, 1);
1732     build_append_int_noprefix(tbl, 0, 1); /* PM2_CNT_LEN */
1733     build_append_int_noprefix(tbl, f->pm_tmr.bit_width / 8, 1); /* PM_TMR_LEN */
1734     /* GPE0_BLK_LEN */
1735     build_append_int_noprefix(tbl, f->gpe0_blk.bit_width / 8, 1);
1736     build_append_int_noprefix(tbl, 0, 1); /* GPE1_BLK_LEN */
1737     build_append_int_noprefix(tbl, 0, 1); /* GPE1_BASE */
1738     build_append_int_noprefix(tbl, 0, 1); /* CST_CNT */
1739     build_append_int_noprefix(tbl, f->plvl2_lat, 2); /* P_LVL2_LAT */
1740     build_append_int_noprefix(tbl, f->plvl3_lat, 2); /* P_LVL3_LAT */
1741     build_append_int_noprefix(tbl, 0, 2); /* FLUSH_SIZE */
1742     build_append_int_noprefix(tbl, 0, 2); /* FLUSH_STRIDE */
1743     build_append_int_noprefix(tbl, 0, 1); /* DUTY_OFFSET */
1744     build_append_int_noprefix(tbl, 0, 1); /* DUTY_WIDTH */
1745     build_append_int_noprefix(tbl, 0, 1); /* DAY_ALRM */
1746     build_append_int_noprefix(tbl, 0, 1); /* MON_ALRM */
1747     build_append_int_noprefix(tbl, f->rtc_century, 1); /* CENTURY */
1748     build_append_int_noprefix(tbl, 0, 2); /* IAPC_BOOT_ARCH */
1749     build_append_int_noprefix(tbl, 0, 1); /* Reserved */
1750     build_append_int_noprefix(tbl, f->flags, 4); /* Flags */
1751 
1752     if (f->rev == 1) {
1753         goto build_hdr;
1754     }
1755 
1756     build_append_gas_from_struct(tbl, &f->reset_reg); /* RESET_REG */
1757     build_append_int_noprefix(tbl, f->reset_val, 1); /* RESET_VALUE */
1758     /* Since ACPI 5.1 */
1759     if ((f->rev >= 6) || ((f->rev == 5) && f->minor_ver > 0)) {
1760         build_append_int_noprefix(tbl, f->arm_boot_arch, 2); /* ARM_BOOT_ARCH */
1761         /* FADT Minor Version */
1762         build_append_int_noprefix(tbl, f->minor_ver, 1);
1763     } else {
1764         build_append_int_noprefix(tbl, 0, 3); /* Reserved upto ACPI 5.0 */
1765     }
1766     build_append_int_noprefix(tbl, 0, 8); /* X_FIRMWARE_CTRL */
1767 
1768     /* XDSDT address to be filled by Guest linker at runtime */
1769     off = tbl->len;
1770     build_append_int_noprefix(tbl, 0, 8); /* X_DSDT */
1771     if (f->xdsdt_tbl_offset) {
1772         bios_linker_loader_add_pointer(linker,
1773             ACPI_BUILD_TABLE_FILE, off, 8,
1774             ACPI_BUILD_TABLE_FILE, *f->xdsdt_tbl_offset);
1775     }
1776 
1777     build_append_gas_from_struct(tbl, &f->pm1a_evt); /* X_PM1a_EVT_BLK */
1778     /* X_PM1b_EVT_BLK */
1779     build_append_gas(tbl, AML_AS_SYSTEM_MEMORY, 0 , 0, 0, 0);
1780     build_append_gas_from_struct(tbl, &f->pm1a_cnt); /* X_PM1a_CNT_BLK */
1781     /* X_PM1b_CNT_BLK */
1782     build_append_gas(tbl, AML_AS_SYSTEM_MEMORY, 0 , 0, 0, 0);
1783     /* X_PM2_CNT_BLK */
1784     build_append_gas(tbl, AML_AS_SYSTEM_MEMORY, 0 , 0, 0, 0);
1785     build_append_gas_from_struct(tbl, &f->pm_tmr); /* X_PM_TMR_BLK */
1786     build_append_gas_from_struct(tbl, &f->gpe0_blk); /* X_GPE0_BLK */
1787     build_append_gas(tbl, AML_AS_SYSTEM_MEMORY, 0 , 0, 0, 0); /* X_GPE1_BLK */
1788 
1789     if (f->rev <= 4) {
1790         goto build_hdr;
1791     }
1792 
1793     /* SLEEP_CONTROL_REG */
1794     build_append_gas(tbl, AML_AS_SYSTEM_MEMORY, 0 , 0, 0, 0);
1795     /* SLEEP_STATUS_REG */
1796     build_append_gas(tbl, AML_AS_SYSTEM_MEMORY, 0 , 0, 0, 0);
1797 
1798     /* TODO: extra fields need to be added to support revisions above rev5 */
1799     assert(f->rev == 5);
1800 
1801 build_hdr:
1802     build_header(linker, tbl, (void *)(tbl->data + fadt_start),
1803                  "FACP", tbl->len - fadt_start, f->rev, oem_id, oem_table_id);
1804 }
1805