]>
Commit | Line | Data |
---|---|---|
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 | ||
c3bdc56c | 22 | #include <glib/gprintf.h> |
19934e0e IM |
23 | #include <stdio.h> |
24 | #include <stdarg.h> | |
25 | #include <assert.h> | |
26 | #include <stdbool.h> | |
eae8bded | 27 | #include <string.h> |
19934e0e | 28 | #include "hw/acpi/aml-build.h" |
0f2707e4 | 29 | #include "qemu/bswap.h" |
dc17ab1d | 30 | #include "qemu/bitops.h" |
658c2718 | 31 | #include "hw/acpi/bios-linker-loader.h" |
19934e0e | 32 | |
af59b35c | 33 | static GArray *build_alloc_array(void) |
19934e0e IM |
34 | { |
35 | return g_array_new(false, true /* clear */, 1); | |
36 | } | |
37 | ||
af59b35c | 38 | static void build_free_array(GArray *array) |
19934e0e IM |
39 | { |
40 | g_array_free(array, true); | |
41 | } | |
42 | ||
af59b35c | 43 | static void build_prepend_byte(GArray *array, uint8_t val) |
19934e0e IM |
44 | { |
45 | g_array_prepend_val(array, val); | |
46 | } | |
47 | ||
af59b35c | 48 | static void build_append_byte(GArray *array, uint8_t val) |
19934e0e IM |
49 | { |
50 | g_array_append_val(array, val); | |
51 | } | |
52 | ||
af59b35c | 53 | static void build_append_array(GArray *array, GArray *val) |
19934e0e IM |
54 | { |
55 | g_array_append_vals(array, val->data, val->len); | |
56 | } | |
57 | ||
58 | #define ACPI_NAMESEG_LEN 4 | |
59 | ||
eae8bded IM |
60 | static void |
61 | build_append_nameseg(GArray *array, const char *seg) | |
19934e0e | 62 | { |
19934e0e | 63 | int len; |
19934e0e | 64 | |
eae8bded | 65 | len = strlen(seg); |
19934e0e IM |
66 | assert(len <= ACPI_NAMESEG_LEN); |
67 | ||
eae8bded | 68 | g_array_append_vals(array, seg, len); |
19934e0e IM |
69 | /* Pad up to ACPI_NAMESEG_LEN characters if necessary. */ |
70 | g_array_append_vals(array, "____", ACPI_NAMESEG_LEN - len); | |
71 | } | |
72 | ||
c167e2e7 | 73 | static void GCC_FMT_ATTR(2, 0) |
eae8bded IM |
74 | build_append_namestringv(GArray *array, const char *format, va_list ap) |
75 | { | |
eae8bded | 76 | char *s; |
eae8bded IM |
77 | char **segs; |
78 | char **segs_iter; | |
79 | int seg_count = 0; | |
80 | ||
c3bdc56c | 81 | s = g_strdup_vprintf(format, ap); |
eae8bded IM |
82 | segs = g_strsplit(s, ".", 0); |
83 | g_free(s); | |
84 | ||
85 | /* count segments */ | |
86 | segs_iter = segs; | |
87 | while (*segs_iter) { | |
88 | ++segs_iter; | |
89 | ++seg_count; | |
90 | } | |
91 | /* | |
92 | * ACPI 5.0 spec: 20.2.2 Name Objects Encoding: | |
93 | * "SegCount can be from 1 to 255" | |
94 | */ | |
95 | assert(seg_count > 0 && seg_count <= 255); | |
96 | ||
97 | /* handle RootPath || PrefixPath */ | |
98 | s = *segs; | |
99 | while (*s == '\\' || *s == '^') { | |
100 | build_append_byte(array, *s); | |
101 | ++s; | |
102 | } | |
103 | ||
104 | switch (seg_count) { | |
105 | case 1: | |
106 | if (!*s) { | |
aea10cde | 107 | build_append_byte(array, 0x00); /* NullName */ |
eae8bded IM |
108 | } else { |
109 | build_append_nameseg(array, s); | |
110 | } | |
111 | break; | |
112 | ||
113 | case 2: | |
114 | build_append_byte(array, 0x2E); /* DualNamePrefix */ | |
115 | build_append_nameseg(array, s); | |
116 | build_append_nameseg(array, segs[1]); | |
117 | break; | |
118 | default: | |
119 | build_append_byte(array, 0x2F); /* MultiNamePrefix */ | |
120 | build_append_byte(array, seg_count); | |
121 | ||
122 | /* handle the 1st segment manually due to prefix/root path */ | |
123 | build_append_nameseg(array, s); | |
124 | ||
125 | /* add the rest of segments */ | |
126 | segs_iter = segs + 1; | |
127 | while (*segs_iter) { | |
128 | build_append_nameseg(array, *segs_iter); | |
129 | ++segs_iter; | |
130 | } | |
131 | break; | |
132 | } | |
133 | g_strfreev(segs); | |
134 | } | |
135 | ||
79272661 | 136 | GCC_FMT_ATTR(2, 3) |
af59b35c | 137 | static void build_append_namestring(GArray *array, const char *format, ...) |
eae8bded IM |
138 | { |
139 | va_list ap; | |
140 | ||
141 | va_start(ap, format); | |
142 | build_append_namestringv(array, format, ap); | |
143 | va_end(ap); | |
144 | } | |
145 | ||
19934e0e IM |
146 | /* 5.4 Definition Block Encoding */ |
147 | enum { | |
148 | PACKAGE_LENGTH_1BYTE_SHIFT = 6, /* Up to 63 - use extra 2 bits. */ | |
149 | PACKAGE_LENGTH_2BYTE_SHIFT = 4, | |
150 | PACKAGE_LENGTH_3BYTE_SHIFT = 12, | |
151 | PACKAGE_LENGTH_4BYTE_SHIFT = 20, | |
152 | }; | |
153 | ||
af59b35c | 154 | static void |
19fff2d4 | 155 | build_prepend_package_length(GArray *package, unsigned length, bool incl_self) |
19934e0e IM |
156 | { |
157 | uint8_t byte; | |
19934e0e IM |
158 | unsigned length_bytes; |
159 | ||
160 | if (length + 1 < (1 << PACKAGE_LENGTH_1BYTE_SHIFT)) { | |
161 | length_bytes = 1; | |
162 | } else if (length + 2 < (1 << PACKAGE_LENGTH_3BYTE_SHIFT)) { | |
163 | length_bytes = 2; | |
164 | } else if (length + 3 < (1 << PACKAGE_LENGTH_4BYTE_SHIFT)) { | |
165 | length_bytes = 3; | |
166 | } else { | |
167 | length_bytes = 4; | |
168 | } | |
169 | ||
19fff2d4 IM |
170 | /* |
171 | * NamedField uses PkgLength encoding but it doesn't include length | |
172 | * of PkgLength itself. | |
173 | */ | |
174 | if (incl_self) { | |
175 | /* | |
176 | * PkgLength is the length of the inclusive length of the data | |
177 | * and PkgLength's length itself when used for terms with | |
178 | * explitit length. | |
179 | */ | |
180 | length += length_bytes; | |
181 | } | |
19934e0e IM |
182 | |
183 | switch (length_bytes) { | |
184 | case 1: | |
185 | byte = length; | |
186 | build_prepend_byte(package, byte); | |
187 | return; | |
188 | case 4: | |
189 | byte = length >> PACKAGE_LENGTH_4BYTE_SHIFT; | |
190 | build_prepend_byte(package, byte); | |
191 | length &= (1 << PACKAGE_LENGTH_4BYTE_SHIFT) - 1; | |
192 | /* fall through */ | |
193 | case 3: | |
194 | byte = length >> PACKAGE_LENGTH_3BYTE_SHIFT; | |
195 | build_prepend_byte(package, byte); | |
196 | length &= (1 << PACKAGE_LENGTH_3BYTE_SHIFT) - 1; | |
197 | /* fall through */ | |
198 | case 2: | |
199 | byte = length >> PACKAGE_LENGTH_2BYTE_SHIFT; | |
200 | build_prepend_byte(package, byte); | |
201 | length &= (1 << PACKAGE_LENGTH_2BYTE_SHIFT) - 1; | |
202 | /* fall through */ | |
203 | } | |
204 | /* | |
205 | * Most significant two bits of byte zero indicate how many following bytes | |
206 | * are in PkgLength encoding. | |
207 | */ | |
208 | byte = ((length_bytes - 1) << PACKAGE_LENGTH_1BYTE_SHIFT) | length; | |
209 | build_prepend_byte(package, byte); | |
210 | } | |
211 | ||
214ae59f IM |
212 | static void |
213 | build_append_pkg_length(GArray *array, unsigned length, bool incl_self) | |
214 | { | |
215 | GArray *tmp = build_alloc_array(); | |
216 | ||
217 | build_prepend_package_length(tmp, length, incl_self); | |
218 | build_append_array(array, tmp); | |
219 | build_free_array(tmp); | |
220 | } | |
221 | ||
af59b35c | 222 | static void build_package(GArray *package, uint8_t op) |
19934e0e | 223 | { |
19fff2d4 | 224 | build_prepend_package_length(package, package->len, true); |
19934e0e IM |
225 | build_prepend_byte(package, op); |
226 | } | |
227 | ||
af59b35c | 228 | static void build_extop_package(GArray *package, uint8_t op) |
19934e0e | 229 | { |
661875e9 | 230 | build_package(package, op); |
19934e0e IM |
231 | build_prepend_byte(package, 0x5B); /* ExtOpPrefix */ |
232 | } | |
233 | ||
295a515d | 234 | static void build_append_int_noprefix(GArray *table, uint64_t value, int size) |
19934e0e | 235 | { |
19934e0e IM |
236 | int i; |
237 | ||
19934e0e IM |
238 | for (i = 0; i < size; ++i) { |
239 | build_append_byte(table, value & 0xFF); | |
240 | value = value >> 8; | |
241 | } | |
242 | } | |
243 | ||
af59b35c | 244 | static void build_append_int(GArray *table, uint64_t value) |
19934e0e IM |
245 | { |
246 | if (value == 0x00) { | |
247 | build_append_byte(table, 0x00); /* ZeroOp */ | |
248 | } else if (value == 0x01) { | |
249 | build_append_byte(table, 0x01); /* OneOp */ | |
250 | } else if (value <= 0xFF) { | |
295a515d IM |
251 | build_append_byte(table, 0x0A); /* BytePrefix */ |
252 | build_append_int_noprefix(table, value, 1); | |
19934e0e | 253 | } else if (value <= 0xFFFF) { |
295a515d IM |
254 | build_append_byte(table, 0x0B); /* WordPrefix */ |
255 | build_append_int_noprefix(table, value, 2); | |
256 | } else if (value <= 0xFFFFFFFF) { | |
257 | build_append_byte(table, 0x0C); /* DWordPrefix */ | |
258 | build_append_int_noprefix(table, value, 4); | |
19934e0e | 259 | } else { |
295a515d IM |
260 | build_append_byte(table, 0x0E); /* QWordPrefix */ |
261 | build_append_int_noprefix(table, value, 8); | |
19934e0e IM |
262 | } |
263 | } | |
0f2707e4 IM |
264 | |
265 | static GPtrArray *alloc_list; | |
266 | ||
267 | static Aml *aml_alloc(void) | |
268 | { | |
269 | Aml *var = g_new0(typeof(*var), 1); | |
270 | ||
271 | g_ptr_array_add(alloc_list, var); | |
272 | var->block_flags = AML_NO_OPCODE; | |
273 | var->buf = build_alloc_array(); | |
274 | return var; | |
275 | } | |
276 | ||
3c054bd5 IM |
277 | static Aml *aml_opcode(uint8_t op) |
278 | { | |
279 | Aml *var = aml_alloc(); | |
280 | ||
281 | var->op = op; | |
282 | var->block_flags = AML_OPCODE; | |
283 | return var; | |
284 | } | |
285 | ||
2ef7c27b IM |
286 | static Aml *aml_bundle(uint8_t op, AmlBlockFlags flags) |
287 | { | |
288 | Aml *var = aml_alloc(); | |
289 | ||
290 | var->op = op; | |
291 | var->block_flags = flags; | |
292 | return var; | |
293 | } | |
294 | ||
2e5feadb | 295 | static void aml_free(gpointer data, gpointer user_data) |
0f2707e4 IM |
296 | { |
297 | Aml *var = data; | |
298 | build_free_array(var->buf); | |
afcf905c | 299 | g_free(var); |
0f2707e4 IM |
300 | } |
301 | ||
302 | Aml *init_aml_allocator(void) | |
303 | { | |
304 | Aml *var; | |
305 | ||
306 | assert(!alloc_list); | |
2e5feadb | 307 | alloc_list = g_ptr_array_new(); |
0f2707e4 IM |
308 | var = aml_alloc(); |
309 | return var; | |
310 | } | |
311 | ||
312 | void free_aml_allocator(void) | |
313 | { | |
2e5feadb | 314 | g_ptr_array_foreach(alloc_list, aml_free, NULL); |
0f2707e4 IM |
315 | g_ptr_array_free(alloc_list, true); |
316 | alloc_list = 0; | |
317 | } | |
318 | ||
319 | /* pack data with DefBuffer encoding */ | |
320 | static void build_buffer(GArray *array, uint8_t op) | |
321 | { | |
322 | GArray *data = build_alloc_array(); | |
323 | ||
324 | build_append_int(data, array->len); | |
325 | g_array_prepend_vals(array, data->data, data->len); | |
326 | build_free_array(data); | |
327 | build_package(array, op); | |
328 | } | |
329 | ||
330 | void aml_append(Aml *parent_ctx, Aml *child) | |
331 | { | |
7d433b0d MT |
332 | GArray *buf = build_alloc_array(); |
333 | build_append_array(buf, child->buf); | |
334 | ||
0f2707e4 IM |
335 | switch (child->block_flags) { |
336 | case AML_OPCODE: | |
337 | build_append_byte(parent_ctx->buf, child->op); | |
338 | break; | |
339 | case AML_EXT_PACKAGE: | |
7d433b0d | 340 | build_extop_package(buf, child->op); |
0f2707e4 IM |
341 | break; |
342 | case AML_PACKAGE: | |
7d433b0d | 343 | build_package(buf, child->op); |
0f2707e4 IM |
344 | break; |
345 | case AML_RES_TEMPLATE: | |
7d433b0d | 346 | build_append_byte(buf, 0x79); /* EndTag */ |
0f2707e4 IM |
347 | /* |
348 | * checksum operations are treated as succeeded if checksum | |
349 | * field is zero. [ACPI Spec 1.0b, 6.4.2.8 End Tag] | |
350 | */ | |
7d433b0d | 351 | build_append_byte(buf, 0); |
0f2707e4 IM |
352 | /* fall through, to pack resources in buffer */ |
353 | case AML_BUFFER: | |
7d433b0d | 354 | build_buffer(buf, child->op); |
0f2707e4 IM |
355 | break; |
356 | case AML_NO_OPCODE: | |
357 | break; | |
358 | default: | |
359 | assert(0); | |
360 | break; | |
361 | } | |
7d433b0d MT |
362 | build_append_array(parent_ctx->buf, buf); |
363 | build_free_array(buf); | |
0f2707e4 | 364 | } |
2ef7c27b IM |
365 | |
366 | /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefScope */ | |
367 | Aml *aml_scope(const char *name_format, ...) | |
368 | { | |
369 | va_list ap; | |
370 | Aml *var = aml_bundle(0x10 /* ScopeOp */, AML_PACKAGE); | |
371 | va_start(ap, name_format); | |
372 | build_append_namestringv(var->buf, name_format, ap); | |
373 | va_end(ap); | |
374 | return var; | |
375 | } | |
be06ebd0 | 376 | |
b25af5ad IM |
377 | /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefReturn */ |
378 | Aml *aml_return(Aml *val) | |
379 | { | |
380 | Aml *var = aml_opcode(0xA4 /* ReturnOp */); | |
381 | aml_append(var, val); | |
382 | return var; | |
383 | } | |
384 | ||
295a515d IM |
385 | /* |
386 | * ACPI 1.0b: 16.2.3 Data Objects Encoding: | |
387 | * encodes: ByteConst, WordConst, DWordConst, QWordConst, ZeroOp, OneOp | |
388 | */ | |
389 | Aml *aml_int(const uint64_t val) | |
390 | { | |
391 | Aml *var = aml_alloc(); | |
392 | build_append_int(var->buf, val); | |
393 | return var; | |
394 | } | |
395 | ||
3c054bd5 IM |
396 | /* |
397 | * helper to construct NameString, which returns Aml object | |
398 | * for using with aml_append or other aml_* terms | |
399 | */ | |
400 | Aml *aml_name(const char *name_format, ...) | |
401 | { | |
402 | va_list ap; | |
403 | Aml *var = aml_alloc(); | |
404 | va_start(ap, name_format); | |
405 | build_append_namestringv(var->buf, name_format, ap); | |
406 | va_end(ap); | |
407 | return var; | |
408 | } | |
409 | ||
410 | /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefName */ | |
411 | Aml *aml_name_decl(const char *name, Aml *val) | |
412 | { | |
413 | Aml *var = aml_opcode(0x08 /* NameOp */); | |
414 | build_append_namestring(var->buf, "%s", name); | |
415 | aml_append(var, val); | |
416 | return var; | |
417 | } | |
418 | ||
7193f3a6 IM |
419 | /* ACPI 1.0b: 16.2.6.1 Arg Objects Encoding */ |
420 | Aml *aml_arg(int pos) | |
421 | { | |
422 | Aml *var; | |
423 | uint8_t op = 0x68 /* ARG0 op */ + pos; | |
424 | ||
425 | assert(pos <= 6); | |
426 | var = aml_opcode(op); | |
427 | return var; | |
428 | } | |
429 | ||
f411199d IM |
430 | /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToInteger */ |
431 | Aml *aml_to_integer(Aml *arg) | |
432 | { | |
433 | Aml *var = aml_opcode(0x99 /* ToIntegerOp */); | |
434 | aml_append(var, arg); | |
435 | build_append_byte(var->buf, 0x00 /* NullNameOp */); | |
436 | return var; | |
437 | } | |
438 | ||
c263b3f7 IM |
439 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefStore */ |
440 | Aml *aml_store(Aml *val, Aml *target) | |
441 | { | |
442 | Aml *var = aml_opcode(0x70 /* StoreOp */); | |
443 | aml_append(var, val); | |
444 | aml_append(var, target); | |
445 | return var; | |
446 | } | |
447 | ||
439e2a6e IM |
448 | /** |
449 | * build_opcode_2arg_dst: | |
450 | * @op: 1-byte opcode | |
451 | * @arg1: 1st operand | |
452 | * @arg2: 2nd operand | |
453 | * @dst: optional target to store to, set to NULL if it's not required | |
454 | * | |
455 | * An internal helper to compose AML terms that have | |
456 | * "Op Operand Operand Target" | |
457 | * pattern. | |
458 | * | |
459 | * Returns: The newly allocated and composed according to patter Aml object. | |
460 | */ | |
461 | static Aml * | |
462 | build_opcode_2arg_dst(uint8_t op, Aml *arg1, Aml *arg2, Aml *dst) | |
926f5aae | 463 | { |
439e2a6e | 464 | Aml *var = aml_opcode(op); |
926f5aae IM |
465 | aml_append(var, arg1); |
466 | aml_append(var, arg2); | |
439e2a6e IM |
467 | if (dst) { |
468 | aml_append(var, dst); | |
469 | } else { | |
470 | build_append_byte(var->buf, 0x00 /* NullNameOp */); | |
471 | } | |
926f5aae IM |
472 | return var; |
473 | } | |
474 | ||
439e2a6e IM |
475 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAnd */ |
476 | Aml *aml_and(Aml *arg1, Aml *arg2) | |
477 | { | |
478 | return build_opcode_2arg_dst(0x7B /* AndOp */, arg1, arg2, NULL); | |
479 | } | |
480 | ||
922cc882 SZ |
481 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefOr */ |
482 | Aml *aml_or(Aml *arg1, Aml *arg2) | |
483 | { | |
439e2a6e | 484 | return build_opcode_2arg_dst(0x7D /* OrOp */, arg1, arg2, NULL); |
922cc882 SZ |
485 | } |
486 | ||
a57ddddd MA |
487 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftLeft */ |
488 | Aml *aml_shiftleft(Aml *arg1, Aml *count) | |
489 | { | |
439e2a6e | 490 | return build_opcode_2arg_dst(0x79 /* ShiftLeftOp */, arg1, count, NULL); |
a57ddddd MA |
491 | } |
492 | ||
f7bd7b8e MA |
493 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftRight */ |
494 | Aml *aml_shiftright(Aml *arg1, Aml *count) | |
495 | { | |
439e2a6e | 496 | return build_opcode_2arg_dst(0x7A /* ShiftRightOp */, arg1, count, NULL); |
f7bd7b8e MA |
497 | } |
498 | ||
96396e28 MA |
499 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLLess */ |
500 | Aml *aml_lless(Aml *arg1, Aml *arg2) | |
501 | { | |
502 | Aml *var = aml_opcode(0x95 /* LLessOp */); | |
503 | aml_append(var, arg1); | |
504 | aml_append(var, arg2); | |
505 | return var; | |
506 | } | |
507 | ||
c08cf070 | 508 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAdd */ |
20ca5208 | 509 | Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst) |
c08cf070 | 510 | { |
20ca5208 | 511 | return build_opcode_2arg_dst(0x72 /* AddOp */, arg1, arg2, dst); |
c08cf070 MA |
512 | } |
513 | ||
7059eb42 IM |
514 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSubtract */ |
515 | Aml *aml_subtract(Aml *arg1, Aml *arg2, Aml *dst) | |
516 | { | |
517 | return build_opcode_2arg_dst(0x74 /* SubtractOp */, arg1, arg2, dst); | |
518 | } | |
519 | ||
af39d536 MA |
520 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIncrement */ |
521 | Aml *aml_increment(Aml *arg) | |
522 | { | |
523 | Aml *var = aml_opcode(0x75 /* IncrementOp */); | |
524 | aml_append(var, arg); | |
525 | return var; | |
526 | } | |
527 | ||
7059eb42 IM |
528 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDecrement */ |
529 | Aml *aml_decrement(Aml *arg) | |
530 | { | |
531 | Aml *var = aml_opcode(0x76 /* DecrementOp */); | |
532 | aml_append(var, arg); | |
533 | return var; | |
534 | } | |
535 | ||
928b8996 MA |
536 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIndex */ |
537 | Aml *aml_index(Aml *arg1, Aml *idx) | |
538 | { | |
439e2a6e | 539 | return build_opcode_2arg_dst(0x88 /* IndexOp */, arg1, idx, NULL); |
928b8996 MA |
540 | } |
541 | ||
34189453 IM |
542 | /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefNotify */ |
543 | Aml *aml_notify(Aml *arg1, Aml *arg2) | |
544 | { | |
545 | Aml *var = aml_opcode(0x86 /* NotifyOp */); | |
546 | aml_append(var, arg1); | |
547 | aml_append(var, arg2); | |
548 | return var; | |
7b38ba9c IM |
549 | } |
550 | ||
551 | /* helper to call method with 1 argument */ | |
552 | Aml *aml_call0(const char *method) | |
553 | { | |
554 | Aml *var = aml_alloc(); | |
555 | build_append_namestring(var->buf, "%s", method); | |
556 | return var; | |
34189453 IM |
557 | } |
558 | ||
3f3992b7 IM |
559 | /* helper to call method with 1 argument */ |
560 | Aml *aml_call1(const char *method, Aml *arg1) | |
561 | { | |
562 | Aml *var = aml_alloc(); | |
563 | build_append_namestring(var->buf, "%s", method); | |
564 | aml_append(var, arg1); | |
565 | return var; | |
566 | } | |
567 | ||
568 | /* helper to call method with 2 arguments */ | |
569 | Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2) | |
570 | { | |
571 | Aml *var = aml_alloc(); | |
572 | build_append_namestring(var->buf, "%s", method); | |
573 | aml_append(var, arg1); | |
574 | aml_append(var, arg2); | |
575 | return var; | |
576 | } | |
577 | ||
578 | /* helper to call method with 3 arguments */ | |
579 | Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3) | |
580 | { | |
581 | Aml *var = aml_alloc(); | |
582 | build_append_namestring(var->buf, "%s", method); | |
583 | aml_append(var, arg1); | |
584 | aml_append(var, arg2); | |
585 | aml_append(var, arg3); | |
586 | return var; | |
587 | } | |
588 | ||
589 | /* helper to call method with 4 arguments */ | |
590 | Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4) | |
591 | { | |
592 | Aml *var = aml_alloc(); | |
593 | build_append_namestring(var->buf, "%s", method); | |
594 | aml_append(var, arg1); | |
595 | aml_append(var, arg2); | |
596 | aml_append(var, arg3); | |
597 | aml_append(var, arg4); | |
598 | return var; | |
599 | } | |
600 | ||
4ecdc746 SZ |
601 | /* |
602 | * ACPI 5.0: 6.4.3.8.1 GPIO Connection Descriptor | |
603 | * Type 1, Large Item Name 0xC | |
604 | */ | |
605 | ||
606 | static Aml *aml_gpio_connection(AmlGpioConnectionType type, | |
607 | AmlConsumerAndProducer con_and_pro, | |
608 | uint8_t flags, AmlPinConfig pin_config, | |
609 | uint16_t output_drive, | |
610 | uint16_t debounce_timeout, | |
611 | const uint32_t pin_list[], uint32_t pin_count, | |
612 | const char *resource_source_name, | |
613 | const uint8_t *vendor_data, | |
614 | uint16_t vendor_data_len) | |
615 | { | |
616 | Aml *var = aml_alloc(); | |
617 | const uint16_t min_desc_len = 0x16; | |
618 | uint16_t resource_source_name_len, length; | |
619 | uint16_t pin_table_offset, resource_source_name_offset, vendor_data_offset; | |
620 | uint32_t i; | |
621 | ||
622 | assert(resource_source_name); | |
623 | resource_source_name_len = strlen(resource_source_name) + 1; | |
624 | length = min_desc_len + resource_source_name_len + vendor_data_len; | |
625 | pin_table_offset = min_desc_len + 1; | |
626 | resource_source_name_offset = pin_table_offset + pin_count * 2; | |
627 | vendor_data_offset = resource_source_name_offset + resource_source_name_len; | |
628 | ||
629 | build_append_byte(var->buf, 0x8C); /* GPIO Connection Descriptor */ | |
630 | build_append_int_noprefix(var->buf, length, 2); /* Length */ | |
631 | build_append_byte(var->buf, 1); /* Revision ID */ | |
632 | build_append_byte(var->buf, type); /* GPIO Connection Type */ | |
633 | /* General Flags (2 bytes) */ | |
634 | build_append_int_noprefix(var->buf, con_and_pro, 2); | |
635 | /* Interrupt and IO Flags (2 bytes) */ | |
636 | build_append_int_noprefix(var->buf, flags, 2); | |
637 | /* Pin Configuration 0 = Default 1 = Pull-up 2 = Pull-down 3 = No Pull */ | |
638 | build_append_byte(var->buf, pin_config); | |
639 | /* Output Drive Strength (2 bytes) */ | |
640 | build_append_int_noprefix(var->buf, output_drive, 2); | |
641 | /* Debounce Timeout (2 bytes) */ | |
642 | build_append_int_noprefix(var->buf, debounce_timeout, 2); | |
643 | /* Pin Table Offset (2 bytes) */ | |
644 | build_append_int_noprefix(var->buf, pin_table_offset, 2); | |
645 | build_append_byte(var->buf, 0); /* Resource Source Index */ | |
646 | /* Resource Source Name Offset (2 bytes) */ | |
647 | build_append_int_noprefix(var->buf, resource_source_name_offset, 2); | |
648 | /* Vendor Data Offset (2 bytes) */ | |
649 | build_append_int_noprefix(var->buf, vendor_data_offset, 2); | |
650 | /* Vendor Data Length (2 bytes) */ | |
651 | build_append_int_noprefix(var->buf, vendor_data_len, 2); | |
652 | /* Pin Number (2n bytes)*/ | |
653 | for (i = 0; i < pin_count; i++) { | |
654 | build_append_int_noprefix(var->buf, pin_list[i], 2); | |
655 | } | |
656 | ||
657 | /* Resource Source Name */ | |
658 | build_append_namestring(var->buf, "%s", resource_source_name); | |
659 | build_append_byte(var->buf, '\0'); | |
660 | ||
661 | /* Vendor-defined Data */ | |
662 | if (vendor_data != NULL) { | |
663 | g_array_append_vals(var->buf, vendor_data, vendor_data_len); | |
664 | } | |
665 | ||
666 | return var; | |
667 | } | |
668 | ||
37d0e980 SZ |
669 | /* |
670 | * ACPI 5.0: 19.5.53 | |
671 | * GpioInt(GPIO Interrupt Connection Resource Descriptor Macro) | |
672 | */ | |
673 | Aml *aml_gpio_int(AmlConsumerAndProducer con_and_pro, | |
674 | AmlLevelAndEdge edge_level, | |
675 | AmlActiveHighAndLow active_level, AmlShared shared, | |
676 | AmlPinConfig pin_config, uint16_t debounce_timeout, | |
677 | const uint32_t pin_list[], uint32_t pin_count, | |
678 | const char *resource_source_name, | |
679 | const uint8_t *vendor_data, uint16_t vendor_data_len) | |
680 | { | |
681 | uint8_t flags = edge_level | (active_level << 1) | (shared << 3); | |
682 | ||
683 | return aml_gpio_connection(AML_INTERRUPT_CONNECTION, con_and_pro, flags, | |
684 | pin_config, 0, debounce_timeout, pin_list, | |
685 | pin_count, resource_source_name, vendor_data, | |
686 | vendor_data_len); | |
687 | } | |
688 | ||
dc17ab1d SZ |
689 | /* |
690 | * ACPI 1.0b: 6.4.3.4 32-Bit Fixed Location Memory Range Descriptor | |
691 | * (Type 1, Large Item Name 0x6) | |
692 | */ | |
693 | Aml *aml_memory32_fixed(uint32_t addr, uint32_t size, | |
694 | AmlReadAndWrite read_and_write) | |
695 | { | |
696 | Aml *var = aml_alloc(); | |
697 | build_append_byte(var->buf, 0x86); /* Memory32Fixed Resource Descriptor */ | |
698 | build_append_byte(var->buf, 9); /* Length, bits[7:0] value = 9 */ | |
699 | build_append_byte(var->buf, 0); /* Length, bits[15:8] value = 0 */ | |
700 | build_append_byte(var->buf, read_and_write); /* Write status, 1 rw 0 ro */ | |
701 | ||
702 | /* Range base address */ | |
703 | build_append_byte(var->buf, extract32(addr, 0, 8)); /* bits[7:0] */ | |
704 | build_append_byte(var->buf, extract32(addr, 8, 8)); /* bits[15:8] */ | |
705 | build_append_byte(var->buf, extract32(addr, 16, 8)); /* bits[23:16] */ | |
706 | build_append_byte(var->buf, extract32(addr, 24, 8)); /* bits[31:24] */ | |
707 | ||
708 | /* Range length */ | |
709 | build_append_byte(var->buf, extract32(size, 0, 8)); /* bits[7:0] */ | |
710 | build_append_byte(var->buf, extract32(size, 8, 8)); /* bits[15:8] */ | |
711 | build_append_byte(var->buf, extract32(size, 16, 8)); /* bits[23:16] */ | |
712 | build_append_byte(var->buf, extract32(size, 24, 8)); /* bits[31:24] */ | |
713 | return var; | |
714 | } | |
715 | ||
205d1d1c SZ |
716 | /* |
717 | * ACPI 5.0: 6.4.3.6 Extended Interrupt Descriptor | |
718 | * Type 1, Large Item Name 0x9 | |
719 | */ | |
720 | Aml *aml_interrupt(AmlConsumerAndProducer con_and_pro, | |
721 | AmlLevelAndEdge level_and_edge, | |
722 | AmlActiveHighAndLow high_and_low, AmlShared shared, | |
45fcf539 | 723 | uint32_t *irq_list, uint8_t irq_count) |
205d1d1c | 724 | { |
45fcf539 | 725 | int i; |
205d1d1c SZ |
726 | Aml *var = aml_alloc(); |
727 | uint8_t irq_flags = con_and_pro | (level_and_edge << 1) | |
728 | | (high_and_low << 2) | (shared << 3); | |
45fcf539 IM |
729 | const int header_bytes_in_len = 2; |
730 | uint16_t len = header_bytes_in_len + irq_count * sizeof(uint32_t); | |
731 | ||
732 | assert(irq_count > 0); | |
205d1d1c SZ |
733 | |
734 | build_append_byte(var->buf, 0x89); /* Extended irq descriptor */ | |
45fcf539 IM |
735 | build_append_byte(var->buf, len & 0xFF); /* Length, bits[7:0] */ |
736 | build_append_byte(var->buf, len >> 8); /* Length, bits[15:8] */ | |
205d1d1c | 737 | build_append_byte(var->buf, irq_flags); /* Interrupt Vector Information. */ |
45fcf539 | 738 | build_append_byte(var->buf, irq_count); /* Interrupt table length */ |
205d1d1c | 739 | |
45fcf539 IM |
740 | /* Interrupt Number List */ |
741 | for (i = 0; i < irq_count; i++) { | |
742 | build_append_int_noprefix(var->buf, irq_list[i], 4); | |
743 | } | |
205d1d1c SZ |
744 | return var; |
745 | } | |
746 | ||
52fa397c IM |
747 | /* ACPI 1.0b: 6.4.2.5 I/O Port Descriptor */ |
748 | Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, | |
749 | uint8_t aln, uint8_t len) | |
750 | { | |
751 | Aml *var = aml_alloc(); | |
752 | build_append_byte(var->buf, 0x47); /* IO port descriptor */ | |
753 | build_append_byte(var->buf, dec); | |
754 | build_append_byte(var->buf, min_base & 0xff); | |
755 | build_append_byte(var->buf, (min_base >> 8) & 0xff); | |
756 | build_append_byte(var->buf, max_base & 0xff); | |
757 | build_append_byte(var->buf, (max_base >> 8) & 0xff); | |
758 | build_append_byte(var->buf, aln); | |
759 | build_append_byte(var->buf, len); | |
760 | return var; | |
761 | } | |
762 | ||
70560453 IM |
763 | /* |
764 | * ACPI 1.0b: 6.4.2.1.1 ASL Macro for IRQ Descriptor | |
765 | * | |
766 | * More verbose description at: | |
767 | * ACPI 5.0: 19.5.64 IRQNoFlags (Interrupt Resource Descriptor Macro) | |
768 | * 6.4.2.1 IRQ Descriptor | |
769 | */ | |
770 | Aml *aml_irq_no_flags(uint8_t irq) | |
771 | { | |
772 | uint16_t irq_mask; | |
773 | Aml *var = aml_alloc(); | |
774 | ||
775 | assert(irq < 16); | |
776 | build_append_byte(var->buf, 0x22); /* IRQ descriptor 2 byte form */ | |
777 | ||
778 | irq_mask = 1U << irq; | |
779 | build_append_byte(var->buf, irq_mask & 0xFF); /* IRQ mask bits[7:0] */ | |
780 | build_append_byte(var->buf, irq_mask >> 8); /* IRQ mask bits[15:8] */ | |
781 | return var; | |
782 | } | |
783 | ||
ea7df04a SZ |
784 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLNot */ |
785 | Aml *aml_lnot(Aml *arg) | |
786 | { | |
787 | Aml *var = aml_opcode(0x92 /* LNotOp */); | |
788 | aml_append(var, arg); | |
789 | return var; | |
790 | } | |
791 | ||
15e44e56 IM |
792 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLEqual */ |
793 | Aml *aml_equal(Aml *arg1, Aml *arg2) | |
794 | { | |
795 | Aml *var = aml_opcode(0x93 /* LequalOp */); | |
796 | aml_append(var, arg1); | |
797 | aml_append(var, arg2); | |
15e44e56 IM |
798 | return var; |
799 | } | |
800 | ||
2d3f667d IM |
801 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLGreaterEqual */ |
802 | Aml *aml_lgreater_equal(Aml *arg1, Aml *arg2) | |
803 | { | |
804 | /* LGreaterEqualOp := LNotOp LLessOp */ | |
805 | Aml *var = aml_opcode(0x92 /* LNotOp */); | |
806 | build_append_byte(var->buf, 0x95 /* LLessOp */); | |
807 | aml_append(var, arg1); | |
808 | aml_append(var, arg2); | |
809 | return var; | |
810 | } | |
811 | ||
32acac9e IM |
812 | /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */ |
813 | Aml *aml_if(Aml *predicate) | |
814 | { | |
815 | Aml *var = aml_bundle(0xA0 /* IfOp */, AML_PACKAGE); | |
816 | aml_append(var, predicate); | |
817 | return var; | |
818 | } | |
819 | ||
467b07df SZ |
820 | /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefElse */ |
821 | Aml *aml_else(void) | |
822 | { | |
823 | Aml *var = aml_bundle(0xA1 /* ElseOp */, AML_PACKAGE); | |
824 | return var; | |
825 | } | |
826 | ||
32d9ca15 MA |
827 | /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefWhile */ |
828 | Aml *aml_while(Aml *predicate) | |
829 | { | |
830 | Aml *var = aml_bundle(0xA2 /* WhileOp */, AML_PACKAGE); | |
831 | aml_append(var, predicate); | |
832 | return var; | |
833 | } | |
834 | ||
ea2407d7 | 835 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */ |
4dbfc881 | 836 | Aml *aml_method(const char *name, int arg_count, AmlSerializeFlag sflag) |
ea2407d7 IM |
837 | { |
838 | Aml *var = aml_bundle(0x14 /* MethodOp */, AML_PACKAGE); | |
4dbfc881 XG |
839 | int methodflags; |
840 | ||
841 | /* | |
842 | * MethodFlags: | |
843 | * bit 0-2: ArgCount (0-7) | |
844 | * bit 3: SerializeFlag | |
845 | * 0: NotSerialized | |
846 | * 1: Serialized | |
847 | * bit 4-7: reserved (must be 0) | |
848 | */ | |
849 | assert(arg_count < 8); | |
850 | methodflags = arg_count | (sflag << 3); | |
851 | ||
ea2407d7 | 852 | build_append_namestring(var->buf, "%s", name); |
4dbfc881 | 853 | build_append_byte(var->buf, methodflags); /* MethodFlags: ArgCount */ |
ea2407d7 IM |
854 | return var; |
855 | } | |
856 | ||
be06ebd0 IM |
857 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */ |
858 | Aml *aml_device(const char *name_format, ...) | |
859 | { | |
860 | va_list ap; | |
861 | Aml *var = aml_bundle(0x82 /* DeviceOp */, AML_EXT_PACKAGE); | |
862 | va_start(ap, name_format); | |
863 | build_append_namestringv(var->buf, name_format, ap); | |
864 | va_end(ap); | |
865 | return var; | |
866 | } | |
3bfa74a7 | 867 | |
ad4a80bc IM |
868 | /* ACPI 1.0b: 6.4.1 ASL Macros for Resource Descriptors */ |
869 | Aml *aml_resource_template(void) | |
870 | { | |
871 | /* ResourceTemplate is a buffer of Resources with EndTag at the end */ | |
872 | Aml *var = aml_bundle(0x11 /* BufferOp */, AML_RES_TEMPLATE); | |
873 | return var; | |
874 | } | |
875 | ||
ed8b5847 SZ |
876 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefBuffer |
877 | * Pass byte_list as NULL to request uninitialized buffer to reserve space. | |
878 | */ | |
879 | Aml *aml_buffer(int buffer_size, uint8_t *byte_list) | |
04b8da54 | 880 | { |
ed8b5847 | 881 | int i; |
04b8da54 | 882 | Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER); |
ed8b5847 SZ |
883 | |
884 | for (i = 0; i < buffer_size; i++) { | |
885 | if (byte_list == NULL) { | |
886 | build_append_byte(var->buf, 0x0); | |
887 | } else { | |
888 | build_append_byte(var->buf, byte_list[i]); | |
889 | } | |
890 | } | |
891 | ||
04b8da54 IM |
892 | return var; |
893 | } | |
894 | ||
3bfa74a7 IM |
895 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefPackage */ |
896 | Aml *aml_package(uint8_t num_elements) | |
897 | { | |
898 | Aml *var = aml_bundle(0x12 /* PackageOp */, AML_PACKAGE); | |
899 | build_append_byte(var->buf, num_elements); | |
900 | return var; | |
901 | } | |
31127938 IM |
902 | |
903 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefOpRegion */ | |
904 | Aml *aml_operation_region(const char *name, AmlRegionSpace rs, | |
905 | uint32_t offset, uint32_t len) | |
906 | { | |
907 | Aml *var = aml_alloc(); | |
908 | build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */ | |
909 | build_append_byte(var->buf, 0x80); /* OpRegionOp */ | |
910 | build_append_namestring(var->buf, "%s", name); | |
911 | build_append_byte(var->buf, rs); | |
912 | build_append_int(var->buf, offset); | |
913 | build_append_int(var->buf, len); | |
914 | return var; | |
915 | } | |
214ae59f IM |
916 | |
917 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: NamedField */ | |
918 | Aml *aml_named_field(const char *name, unsigned length) | |
919 | { | |
920 | Aml *var = aml_alloc(); | |
921 | build_append_nameseg(var->buf, name); | |
922 | build_append_pkg_length(var->buf, length, false); | |
923 | return var; | |
924 | } | |
925 | ||
e2ea299b IM |
926 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: ReservedField */ |
927 | Aml *aml_reserved_field(unsigned length) | |
928 | { | |
929 | Aml *var = aml_alloc(); | |
930 | /* ReservedField := 0x00 PkgLength */ | |
931 | build_append_byte(var->buf, 0x00); | |
932 | build_append_pkg_length(var->buf, length, false); | |
933 | return var; | |
934 | } | |
935 | ||
214ae59f | 936 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefField */ |
af509897 | 937 | Aml *aml_field(const char *name, AmlAccessType type, AmlUpdateRule rule) |
214ae59f IM |
938 | { |
939 | Aml *var = aml_bundle(0x81 /* FieldOp */, AML_EXT_PACKAGE); | |
af509897 ZG |
940 | uint8_t flags = rule << 5 | type; |
941 | ||
214ae59f IM |
942 | build_append_namestring(var->buf, "%s", name); |
943 | build_append_byte(var->buf, flags); | |
944 | return var; | |
945 | } | |
b8a5d689 | 946 | |
7e192a38 IM |
947 | static |
948 | Aml *create_field_common(int opcode, Aml *srcbuf, Aml *index, const char *name) | |
ed8176a3 | 949 | { |
7e192a38 | 950 | Aml *var = aml_opcode(opcode); |
ed8176a3 SZ |
951 | aml_append(var, srcbuf); |
952 | aml_append(var, index); | |
953 | build_append_namestring(var->buf, "%s", name); | |
954 | return var; | |
955 | } | |
956 | ||
7e192a38 IM |
957 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefCreateDWordField */ |
958 | Aml *aml_create_dword_field(Aml *srcbuf, Aml *index, const char *name) | |
959 | { | |
960 | return create_field_common(0x8A /* CreateDWordFieldOp */, | |
961 | srcbuf, index, name); | |
962 | } | |
963 | ||
964 | /* ACPI 2.0a: 17.2.4.2 Named Objects Encoding: DefCreateQWordField */ | |
965 | Aml *aml_create_qword_field(Aml *srcbuf, Aml *index, const char *name) | |
966 | { | |
967 | return create_field_common(0x8F /* CreateQWordFieldOp */, | |
968 | srcbuf, index, name); | |
969 | } | |
970 | ||
d5e5830f IM |
971 | /* ACPI 1.0b: 16.2.3 Data Objects Encoding: String */ |
972 | Aml *aml_string(const char *name_format, ...) | |
973 | { | |
974 | Aml *var = aml_opcode(0x0D /* StringPrefix */); | |
c3bdc56c | 975 | va_list ap; |
d5e5830f IM |
976 | char *s; |
977 | int len; | |
978 | ||
979 | va_start(ap, name_format); | |
c3bdc56c | 980 | len = g_vasprintf(&s, name_format, ap); |
d5e5830f IM |
981 | va_end(ap); |
982 | ||
c3bdc56c | 983 | g_array_append_vals(var->buf, s, len + 1); |
d5e5830f IM |
984 | g_free(s); |
985 | ||
986 | return var; | |
987 | } | |
988 | ||
b8a5d689 IM |
989 | /* ACPI 1.0b: 16.2.6.2 Local Objects Encoding */ |
990 | Aml *aml_local(int num) | |
991 | { | |
992 | Aml *var; | |
993 | uint8_t op = 0x60 /* Local0Op */ + num; | |
994 | ||
995 | assert(num <= 7); | |
996 | var = aml_opcode(op); | |
997 | return var; | |
998 | } | |
a678508e IM |
999 | |
1000 | /* ACPI 2.0a: 17.2.2 Data Objects Encoding: DefVarPackage */ | |
1001 | Aml *aml_varpackage(uint32_t num_elements) | |
1002 | { | |
1003 | Aml *var = aml_bundle(0x13 /* VarPackageOp */, AML_PACKAGE); | |
1004 | build_append_int(var->buf, num_elements); | |
1005 | return var; | |
1006 | } | |
3dd15643 IM |
1007 | |
1008 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefProcessor */ | |
1009 | Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len, | |
1010 | const char *name_format, ...) | |
1011 | { | |
1012 | va_list ap; | |
1013 | Aml *var = aml_bundle(0x83 /* ProcessorOp */, AML_EXT_PACKAGE); | |
1014 | va_start(ap, name_format); | |
1015 | build_append_namestringv(var->buf, name_format, ap); | |
1016 | va_end(ap); | |
1017 | build_append_byte(var->buf, proc_id); /* ProcID */ | |
1018 | build_append_int_noprefix(var->buf, pblk_addr, sizeof(pblk_addr)); | |
1019 | build_append_byte(var->buf, pblk_len); /* PblkLen */ | |
1020 | return var; | |
1021 | } | |
a7891dac IM |
1022 | |
1023 | static uint8_t Hex2Digit(char c) | |
1024 | { | |
1025 | if (c >= 'A') { | |
1026 | return c - 'A' + 10; | |
1027 | } | |
1028 | ||
1029 | return c - '0'; | |
1030 | } | |
1031 | ||
1032 | /* ACPI 1.0b: 15.2.3.6.4.1 EISAID Macro - Convert EISA ID String To Integer */ | |
1033 | Aml *aml_eisaid(const char *str) | |
1034 | { | |
1035 | Aml *var = aml_alloc(); | |
1036 | uint32_t id; | |
1037 | ||
1038 | g_assert(strlen(str) == 7); | |
1039 | id = (str[0] - 0x40) << 26 | | |
1040 | (str[1] - 0x40) << 21 | | |
1041 | (str[2] - 0x40) << 16 | | |
1042 | Hex2Digit(str[3]) << 12 | | |
1043 | Hex2Digit(str[4]) << 8 | | |
1044 | Hex2Digit(str[5]) << 4 | | |
1045 | Hex2Digit(str[6]); | |
1046 | ||
1047 | build_append_byte(var->buf, 0x0C); /* DWordPrefix */ | |
1048 | build_append_int_noprefix(var->buf, bswap32(id), sizeof(id)); | |
1049 | return var; | |
1050 | } | |
6ece7053 IM |
1051 | |
1052 | /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor: bytes 3-5 */ | |
1053 | static Aml *aml_as_desc_header(AmlResourceType type, AmlMinFixed min_fixed, | |
1054 | AmlMaxFixed max_fixed, AmlDecode dec, | |
1055 | uint8_t type_flags) | |
1056 | { | |
1057 | uint8_t flags = max_fixed | min_fixed | dec; | |
1058 | Aml *var = aml_alloc(); | |
1059 | ||
1060 | build_append_byte(var->buf, type); | |
1061 | build_append_byte(var->buf, flags); | |
1062 | build_append_byte(var->buf, type_flags); /* Type Specific Flags */ | |
1063 | return var; | |
1064 | } | |
1065 | ||
1066 | /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor */ | |
1067 | static Aml *aml_word_as_desc(AmlResourceType type, AmlMinFixed min_fixed, | |
1068 | AmlMaxFixed max_fixed, AmlDecode dec, | |
1069 | uint16_t addr_gran, uint16_t addr_min, | |
1070 | uint16_t addr_max, uint16_t addr_trans, | |
1071 | uint16_t len, uint8_t type_flags) | |
1072 | { | |
1073 | Aml *var = aml_alloc(); | |
1074 | ||
1075 | build_append_byte(var->buf, 0x88); /* Word Address Space Descriptor */ | |
1076 | /* minimum length since we do not encode optional fields */ | |
1077 | build_append_byte(var->buf, 0x0D); | |
1078 | build_append_byte(var->buf, 0x0); | |
1079 | ||
1080 | aml_append(var, | |
1081 | aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags)); | |
1082 | build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran)); | |
1083 | build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min)); | |
1084 | build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max)); | |
1085 | build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans)); | |
1086 | build_append_int_noprefix(var->buf, len, sizeof(len)); | |
1087 | return var; | |
1088 | } | |
1089 | ||
1090 | /* ACPI 1.0b: 6.4.3.5.3 DWord Address Space Descriptor */ | |
1091 | static Aml *aml_dword_as_desc(AmlResourceType type, AmlMinFixed min_fixed, | |
1092 | AmlMaxFixed max_fixed, AmlDecode dec, | |
1093 | uint32_t addr_gran, uint32_t addr_min, | |
1094 | uint32_t addr_max, uint32_t addr_trans, | |
1095 | uint32_t len, uint8_t type_flags) | |
1096 | { | |
1097 | Aml *var = aml_alloc(); | |
1098 | ||
1099 | build_append_byte(var->buf, 0x87); /* DWord Address Space Descriptor */ | |
1100 | /* minimum length since we do not encode optional fields */ | |
1101 | build_append_byte(var->buf, 23); | |
1102 | build_append_byte(var->buf, 0x0); | |
1103 | ||
1104 | ||
1105 | aml_append(var, | |
1106 | aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags)); | |
1107 | build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran)); | |
1108 | build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min)); | |
1109 | build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max)); | |
1110 | build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans)); | |
1111 | build_append_int_noprefix(var->buf, len, sizeof(len)); | |
1112 | return var; | |
1113 | } | |
1114 | ||
1115 | /* ACPI 1.0b: 6.4.3.5.1 QWord Address Space Descriptor */ | |
1116 | static Aml *aml_qword_as_desc(AmlResourceType type, AmlMinFixed min_fixed, | |
1117 | AmlMaxFixed max_fixed, AmlDecode dec, | |
1118 | uint64_t addr_gran, uint64_t addr_min, | |
1119 | uint64_t addr_max, uint64_t addr_trans, | |
1120 | uint64_t len, uint8_t type_flags) | |
1121 | { | |
1122 | Aml *var = aml_alloc(); | |
1123 | ||
1124 | build_append_byte(var->buf, 0x8A); /* QWord Address Space Descriptor */ | |
1125 | /* minimum length since we do not encode optional fields */ | |
1126 | build_append_byte(var->buf, 0x2B); | |
1127 | build_append_byte(var->buf, 0x0); | |
1128 | ||
1129 | aml_append(var, | |
1130 | aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags)); | |
1131 | build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran)); | |
1132 | build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min)); | |
1133 | build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max)); | |
1134 | build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans)); | |
1135 | build_append_int_noprefix(var->buf, len, sizeof(len)); | |
1136 | return var; | |
1137 | } | |
1138 | ||
1139 | /* | |
1140 | * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor | |
1141 | * | |
1142 | * More verbose description at: | |
1143 | * ACPI 5.0: 19.5.141 WordBusNumber (Word Bus Number Resource Descriptor Macro) | |
1144 | */ | |
1145 | Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, | |
1146 | AmlDecode dec, uint16_t addr_gran, | |
1147 | uint16_t addr_min, uint16_t addr_max, | |
1148 | uint16_t addr_trans, uint16_t len) | |
1149 | ||
1150 | { | |
ff80dc7f | 1151 | return aml_word_as_desc(AML_BUS_NUMBER_RANGE, min_fixed, max_fixed, dec, |
6ece7053 IM |
1152 | addr_gran, addr_min, addr_max, addr_trans, len, 0); |
1153 | } | |
1154 | ||
1155 | /* | |
1156 | * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor | |
1157 | * | |
1158 | * More verbose description at: | |
1159 | * ACPI 5.0: 19.5.142 WordIO (Word IO Resource Descriptor Macro) | |
1160 | */ | |
1161 | Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, | |
1162 | AmlDecode dec, AmlISARanges isa_ranges, | |
1163 | uint16_t addr_gran, uint16_t addr_min, | |
1164 | uint16_t addr_max, uint16_t addr_trans, | |
1165 | uint16_t len) | |
1166 | ||
1167 | { | |
ff80dc7f | 1168 | return aml_word_as_desc(AML_IO_RANGE, min_fixed, max_fixed, dec, |
6ece7053 IM |
1169 | addr_gran, addr_min, addr_max, addr_trans, len, |
1170 | isa_ranges); | |
1171 | } | |
1172 | ||
616ef329 SZ |
1173 | /* |
1174 | * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Descriptor | |
1175 | * | |
1176 | * More verbose description at: | |
1177 | * ACPI 5.0: 19.5.33 DWordIO (DWord IO Resource Descriptor Macro) | |
1178 | */ | |
1179 | Aml *aml_dword_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, | |
1180 | AmlDecode dec, AmlISARanges isa_ranges, | |
1181 | uint32_t addr_gran, uint32_t addr_min, | |
1182 | uint32_t addr_max, uint32_t addr_trans, | |
1183 | uint32_t len) | |
1184 | ||
1185 | { | |
1186 | return aml_dword_as_desc(AML_IO_RANGE, min_fixed, max_fixed, dec, | |
1187 | addr_gran, addr_min, addr_max, addr_trans, len, | |
1188 | isa_ranges); | |
1189 | } | |
1190 | ||
6ece7053 IM |
1191 | /* |
1192 | * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Space Descriptor | |
1193 | * | |
1194 | * More verbose description at: | |
1195 | * ACPI 5.0: 19.5.34 DWordMemory (DWord Memory Resource Descriptor Macro) | |
1196 | */ | |
1197 | Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed, | |
ff80dc7f | 1198 | AmlMaxFixed max_fixed, AmlCacheable cacheable, |
6ece7053 IM |
1199 | AmlReadAndWrite read_and_write, |
1200 | uint32_t addr_gran, uint32_t addr_min, | |
1201 | uint32_t addr_max, uint32_t addr_trans, | |
1202 | uint32_t len) | |
1203 | { | |
1204 | uint8_t flags = read_and_write | (cacheable << 1); | |
1205 | ||
ff80dc7f | 1206 | return aml_dword_as_desc(AML_MEMORY_RANGE, min_fixed, max_fixed, |
6ece7053 IM |
1207 | dec, addr_gran, addr_min, addr_max, |
1208 | addr_trans, len, flags); | |
1209 | } | |
1210 | ||
1211 | /* | |
1212 | * ACPI 1.0b: 6.4.3.5.2 ASL Macros for QWORD Address Space Descriptor | |
1213 | * | |
1214 | * More verbose description at: | |
1215 | * ACPI 5.0: 19.5.102 QWordMemory (QWord Memory Resource Descriptor Macro) | |
1216 | */ | |
1217 | Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed, | |
ff80dc7f | 1218 | AmlMaxFixed max_fixed, AmlCacheable cacheable, |
6ece7053 IM |
1219 | AmlReadAndWrite read_and_write, |
1220 | uint64_t addr_gran, uint64_t addr_min, | |
1221 | uint64_t addr_max, uint64_t addr_trans, | |
1222 | uint64_t len) | |
1223 | { | |
1224 | uint8_t flags = read_and_write | (cacheable << 1); | |
1225 | ||
ff80dc7f | 1226 | return aml_qword_as_desc(AML_MEMORY_RANGE, min_fixed, max_fixed, |
6ece7053 IM |
1227 | dec, addr_gran, addr_min, addr_max, |
1228 | addr_trans, len, flags); | |
1229 | } | |
658c2718 | 1230 | |
b930fb9d SZ |
1231 | static uint8_t Hex2Byte(const char *src) |
1232 | { | |
1233 | int hi, lo; | |
1234 | ||
1235 | hi = Hex2Digit(src[0]); | |
1236 | assert(hi >= 0); | |
1237 | assert(hi <= 15); | |
1238 | ||
1239 | lo = Hex2Digit(src[1]); | |
1240 | assert(lo >= 0); | |
1241 | assert(lo <= 15); | |
1242 | return (hi << 4) | lo; | |
1243 | } | |
1244 | ||
1245 | /* | |
1246 | * ACPI 3.0: 17.5.124 ToUUID (Convert String to UUID Macro) | |
1247 | * e.g. UUID: aabbccdd-eeff-gghh-iijj-kkllmmnnoopp | |
1248 | * call aml_touuid("aabbccdd-eeff-gghh-iijj-kkllmmnnoopp"); | |
1249 | */ | |
1250 | Aml *aml_touuid(const char *uuid) | |
1251 | { | |
1252 | Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER); | |
1253 | ||
1254 | assert(strlen(uuid) == 36); | |
1255 | assert(uuid[8] == '-'); | |
1256 | assert(uuid[13] == '-'); | |
1257 | assert(uuid[18] == '-'); | |
1258 | assert(uuid[23] == '-'); | |
1259 | ||
1260 | build_append_byte(var->buf, Hex2Byte(uuid + 6)); /* dd - at offset 00 */ | |
1261 | build_append_byte(var->buf, Hex2Byte(uuid + 4)); /* cc - at offset 01 */ | |
1262 | build_append_byte(var->buf, Hex2Byte(uuid + 2)); /* bb - at offset 02 */ | |
1263 | build_append_byte(var->buf, Hex2Byte(uuid + 0)); /* aa - at offset 03 */ | |
1264 | ||
1265 | build_append_byte(var->buf, Hex2Byte(uuid + 11)); /* ff - at offset 04 */ | |
1266 | build_append_byte(var->buf, Hex2Byte(uuid + 9)); /* ee - at offset 05 */ | |
1267 | ||
1268 | build_append_byte(var->buf, Hex2Byte(uuid + 16)); /* hh - at offset 06 */ | |
1269 | build_append_byte(var->buf, Hex2Byte(uuid + 14)); /* gg - at offset 07 */ | |
1270 | ||
1271 | build_append_byte(var->buf, Hex2Byte(uuid + 19)); /* ii - at offset 08 */ | |
1272 | build_append_byte(var->buf, Hex2Byte(uuid + 21)); /* jj - at offset 09 */ | |
1273 | ||
1274 | build_append_byte(var->buf, Hex2Byte(uuid + 24)); /* kk - at offset 10 */ | |
1275 | build_append_byte(var->buf, Hex2Byte(uuid + 26)); /* ll - at offset 11 */ | |
1276 | build_append_byte(var->buf, Hex2Byte(uuid + 28)); /* mm - at offset 12 */ | |
1277 | build_append_byte(var->buf, Hex2Byte(uuid + 30)); /* nn - at offset 13 */ | |
1278 | build_append_byte(var->buf, Hex2Byte(uuid + 32)); /* oo - at offset 14 */ | |
1279 | build_append_byte(var->buf, Hex2Byte(uuid + 34)); /* pp - at offset 15 */ | |
1280 | ||
1281 | return var; | |
1282 | } | |
1283 | ||
e1f776c4 SZ |
1284 | /* |
1285 | * ACPI 2.0b: 16.2.3.6.4.3 Unicode Macro (Convert Ascii String To Unicode) | |
1286 | */ | |
1287 | Aml *aml_unicode(const char *str) | |
1288 | { | |
1289 | int i = 0; | |
1290 | Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER); | |
1291 | ||
1292 | do { | |
1293 | build_append_byte(var->buf, str[i]); | |
1294 | build_append_byte(var->buf, 0); | |
1295 | i++; | |
1296 | } while (i <= strlen(str)); | |
1297 | ||
1298 | return var; | |
1299 | } | |
1300 | ||
95cb0661 XG |
1301 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDerefOf */ |
1302 | Aml *aml_derefof(Aml *arg) | |
1303 | { | |
1304 | Aml *var = aml_opcode(0x83 /* DerefOfOp */); | |
1305 | aml_append(var, arg); | |
1306 | return var; | |
1307 | } | |
1308 | ||
52483d14 XG |
1309 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSizeOf */ |
1310 | Aml *aml_sizeof(Aml *arg) | |
1311 | { | |
1312 | Aml *var = aml_opcode(0x87 /* SizeOfOp */); | |
1313 | aml_append(var, arg); | |
1314 | return var; | |
1315 | } | |
1316 | ||
6e1db3f2 XG |
1317 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMutex */ |
1318 | Aml *aml_mutex(const char *name, uint8_t sync_level) | |
1319 | { | |
1320 | Aml *var = aml_alloc(); | |
1321 | build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */ | |
1322 | build_append_byte(var->buf, 0x01); /* MutexOp */ | |
1323 | build_append_namestring(var->buf, "%s", name); | |
1324 | assert(!(sync_level & 0xF0)); | |
1325 | build_append_byte(var->buf, sync_level); | |
1326 | return var; | |
1327 | } | |
1328 | ||
1329 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAcquire */ | |
1330 | Aml *aml_acquire(Aml *mutex, uint16_t timeout) | |
1331 | { | |
1332 | Aml *var = aml_alloc(); | |
1333 | build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */ | |
1334 | build_append_byte(var->buf, 0x23); /* AcquireOp */ | |
1335 | aml_append(var, mutex); | |
1336 | build_append_int_noprefix(var->buf, timeout, sizeof(timeout)); | |
1337 | return var; | |
1338 | } | |
1339 | ||
1340 | /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefRelease */ | |
1341 | Aml *aml_release(Aml *mutex) | |
1342 | { | |
1343 | Aml *var = aml_alloc(); | |
1344 | build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */ | |
1345 | build_append_byte(var->buf, 0x27); /* ReleaseOp */ | |
1346 | aml_append(var, mutex); | |
1347 | return var; | |
1348 | } | |
1349 | ||
658c2718 SZ |
1350 | void |
1351 | build_header(GArray *linker, GArray *table_data, | |
8870ca0e XG |
1352 | AcpiTableHeader *h, const char *sig, int len, uint8_t rev, |
1353 | const char *oem_table_id) | |
658c2718 SZ |
1354 | { |
1355 | memcpy(&h->signature, sig, 4); | |
1356 | h->length = cpu_to_le32(len); | |
1357 | h->revision = rev; | |
1358 | memcpy(h->oem_id, ACPI_BUILD_APPNAME6, 6); | |
8870ca0e XG |
1359 | |
1360 | if (oem_table_id) { | |
1361 | strncpy((char *)h->oem_table_id, oem_table_id, sizeof(h->oem_table_id)); | |
1362 | } else { | |
1363 | memcpy(h->oem_table_id, ACPI_BUILD_APPNAME4, 4); | |
1364 | memcpy(h->oem_table_id + 4, sig, 4); | |
1365 | } | |
1366 | ||
658c2718 SZ |
1367 | h->oem_revision = cpu_to_le32(1); |
1368 | memcpy(h->asl_compiler_id, ACPI_BUILD_APPNAME4, 4); | |
1369 | h->asl_compiler_revision = cpu_to_le32(1); | |
1370 | h->checksum = 0; | |
1371 | /* Checksum to be filled in by Guest linker */ | |
1372 | bios_linker_loader_add_checksum(linker, ACPI_BUILD_TABLE_FILE, | |
1373 | table_data->data, h, len, &h->checksum); | |
1374 | } | |
1375 | ||
1376 | void *acpi_data_push(GArray *table_data, unsigned size) | |
1377 | { | |
1378 | unsigned off = table_data->len; | |
1379 | g_array_set_size(table_data, off + size); | |
1380 | return table_data->data + off; | |
1381 | } | |
1382 | ||
1383 | unsigned acpi_data_len(GArray *table) | |
1384 | { | |
658c2718 | 1385 | assert(g_array_get_element_size(table) == 1); |
658c2718 SZ |
1386 | return table->len; |
1387 | } | |
1388 | ||
1389 | void acpi_add_table(GArray *table_offsets, GArray *table_data) | |
1390 | { | |
1391 | uint32_t offset = cpu_to_le32(table_data->len); | |
1392 | g_array_append_val(table_offsets, offset); | |
1393 | } | |
1394 | ||
1395 | void acpi_build_tables_init(AcpiBuildTables *tables) | |
1396 | { | |
1397 | tables->rsdp = g_array_new(false, true /* clear */, 1); | |
1398 | tables->table_data = g_array_new(false, true /* clear */, 1); | |
1399 | tables->tcpalog = g_array_new(false, true /* clear */, 1); | |
1400 | tables->linker = bios_linker_loader_init(); | |
1401 | } | |
1402 | ||
1403 | void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre) | |
1404 | { | |
1405 | void *linker_data = bios_linker_loader_cleanup(tables->linker); | |
1406 | g_free(linker_data); | |
1407 | g_array_free(tables->rsdp, true); | |
1408 | g_array_free(tables->table_data, true); | |
1409 | g_array_free(tables->tcpalog, mfre); | |
1410 | } | |
243bdb79 SZ |
1411 | |
1412 | /* Build rsdt table */ | |
1413 | void | |
1414 | build_rsdt(GArray *table_data, GArray *linker, GArray *table_offsets) | |
1415 | { | |
1416 | AcpiRsdtDescriptorRev1 *rsdt; | |
1417 | size_t rsdt_len; | |
1418 | int i; | |
1419 | const int table_data_len = (sizeof(uint32_t) * table_offsets->len); | |
1420 | ||
1421 | rsdt_len = sizeof(*rsdt) + table_data_len; | |
1422 | rsdt = acpi_data_push(table_data, rsdt_len); | |
1423 | memcpy(rsdt->table_offset_entry, table_offsets->data, table_data_len); | |
1424 | for (i = 0; i < table_offsets->len; ++i) { | |
1425 | /* rsdt->table_offset_entry to be filled by Guest linker */ | |
1426 | bios_linker_loader_add_pointer(linker, | |
1427 | ACPI_BUILD_TABLE_FILE, | |
1428 | ACPI_BUILD_TABLE_FILE, | |
1429 | table_data, &rsdt->table_offset_entry[i], | |
1430 | sizeof(uint32_t)); | |
1431 | } | |
1432 | build_header(linker, table_data, | |
8870ca0e | 1433 | (void *)rsdt, "RSDT", rsdt_len, 1, NULL); |
243bdb79 | 1434 | } |