]>
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 | ||
b6a0aa05 | 22 | #include "qemu/osdep.h" |
c3bdc56c | 23 | #include <glib/gprintf.h> |
19934e0e | 24 | #include "hw/acpi/aml-build.h" |
0f2707e4 | 25 | #include "qemu/bswap.h" |
dc17ab1d | 26 | #include "qemu/bitops.h" |
0f203430 | 27 | #include "sysemu/numa.h" |
19934e0e | 28 | |
af59b35c | 29 | static GArray *build_alloc_array(void) |
19934e0e IM |
30 | { |
31 | return g_array_new(false, true /* clear */, 1); | |
32 | } | |
33 | ||
af59b35c | 34 | static void build_free_array(GArray *array) |
19934e0e IM |
35 | { |
36 | g_array_free(array, true); | |
37 | } | |
38 | ||
af59b35c | 39 | static void build_prepend_byte(GArray *array, uint8_t val) |
19934e0e IM |
40 | { |
41 | g_array_prepend_val(array, val); | |
42 | } | |
43 | ||
af59b35c | 44 | static void build_append_byte(GArray *array, uint8_t val) |
19934e0e IM |
45 | { |
46 | g_array_append_val(array, val); | |
47 | } | |
48 | ||
af59b35c | 49 | static void build_append_array(GArray *array, GArray *val) |
19934e0e IM |
50 | { |
51 | g_array_append_vals(array, val->data, val->len); | |
52 | } | |
53 | ||
54 | #define ACPI_NAMESEG_LEN 4 | |
55 | ||
eae8bded IM |
56 | static void |
57 | build_append_nameseg(GArray *array, const char *seg) | |
19934e0e | 58 | { |
19934e0e | 59 | int len; |
19934e0e | 60 | |
eae8bded | 61 | len = strlen(seg); |
19934e0e IM |
62 | assert(len <= ACPI_NAMESEG_LEN); |
63 | ||
eae8bded | 64 | g_array_append_vals(array, seg, len); |
19934e0e IM |
65 | /* Pad up to ACPI_NAMESEG_LEN characters if necessary. */ |
66 | g_array_append_vals(array, "____", ACPI_NAMESEG_LEN - len); | |
67 | } | |
68 | ||
c167e2e7 | 69 | static void GCC_FMT_ATTR(2, 0) |
eae8bded IM |
70 | build_append_namestringv(GArray *array, const char *format, va_list ap) |
71 | { | |
eae8bded | 72 | char *s; |
eae8bded IM |
73 | char **segs; |
74 | char **segs_iter; | |
75 | int seg_count = 0; | |
76 | ||
c3bdc56c | 77 | s = g_strdup_vprintf(format, ap); |
eae8bded IM |
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) { | |
aea10cde | 103 | build_append_byte(array, 0x00); /* NullName */ |
eae8bded IM |
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 | ||
79272661 | 132 | GCC_FMT_ATTR(2, 3) |
af59b35c | 133 | static void build_append_namestring(GArray *array, const char *format, ...) |
eae8bded IM |
134 | { |
135 | va_list ap; | |
136 | ||
137 | va_start(ap, format); | |
138 | build_append_namestringv(array, format, ap); | |
139 | va_end(ap); | |
140 | } | |
141 | ||
19934e0e IM |
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 | ||
af59b35c | 150 | static void |
19fff2d4 | 151 | build_prepend_package_length(GArray *package, unsigned length, bool incl_self) |
19934e0e IM |
152 | { |
153 | uint8_t byte; | |
19934e0e IM |
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 | ||
19fff2d4 IM |
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 | } | |
19934e0e IM |
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 | ||
214ae59f IM |
208 | static void |
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 | ||
af59b35c | 218 | static void build_package(GArray *package, uint8_t op) |
19934e0e | 219 | { |
19fff2d4 | 220 | build_prepend_package_length(package, package->len, true); |
19934e0e IM |
221 | build_prepend_byte(package, op); |
222 | } | |
223 | ||
af59b35c | 224 | static void build_extop_package(GArray *package, uint8_t op) |
19934e0e | 225 | { |
661875e9 | 226 | build_package(package, op); |
19934e0e IM |
227 | build_prepend_byte(package, 0x5B); /* ExtOpPrefix */ |
228 | } | |
229 | ||
fb9f5926 | 230 | void build_append_int_noprefix(GArray *table, uint64_t value, int size) |
19934e0e | 231 | { |
19934e0e IM |
232 | int i; |
233 | ||
19934e0e IM |
234 | for (i = 0; i < size; ++i) { |
235 | build_append_byte(table, value & 0xFF); | |
236 | value = value >> 8; | |
237 | } | |
238 | } | |
239 | ||
af59b35c | 240 | static void build_append_int(GArray *table, uint64_t value) |
19934e0e IM |
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) { | |
295a515d IM |
247 | build_append_byte(table, 0x0A); /* BytePrefix */ |
248 | build_append_int_noprefix(table, value, 1); | |
19934e0e | 249 | } else if (value <= 0xFFFF) { |
295a515d IM |
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); | |
19934e0e | 255 | } else { |
295a515d IM |
256 | build_append_byte(table, 0x0E); /* QWordPrefix */ |
257 | build_append_int_noprefix(table, value, 8); | |
19934e0e IM |
258 | } |
259 | } | |
0f2707e4 | 260 | |
f2035491 MT |
261 | /* |
262 | * Build NAME(XXXX, 0x00000000) where 0x00000000 is encoded as a dword, | |
263 | * and return the offset to 0x00000000 for runtime patching. | |
264 | * | |
265 | * Warning: runtime patching is best avoided. Only use this as | |
266 | * a replacement for DataTableRegion (for guests that don't | |
267 | * support it). | |
268 | */ | |
269 | int | |
270 | build_append_named_dword(GArray *array, const char *name_format, ...) | |
271 | { | |
272 | int offset; | |
273 | va_list ap; | |
274 | ||
275 | build_append_byte(array, 0x08); /* NameOp */ | |
276 | va_start(ap, name_format); | |
277 | build_append_namestringv(array, name_format, ap); | |
278 | va_end(ap); | |
279 | ||
280 | build_append_byte(array, 0x0C); /* DWordPrefix */ | |
281 | ||
282 | offset = array->len; | |
283 | build_append_int_noprefix(array, 0x00000000, 4); | |
284 | assert(array->len == offset + 4); | |
285 | ||
286 | return offset; | |
287 | } | |
288 | ||
0f2707e4 IM |
289 | static GPtrArray *alloc_list; |
290 | ||
291 | static Aml *aml_alloc(void) | |
292 | { | |
293 | Aml *var = g_new0(typeof(*var), 1); | |
294 | ||
295 | g_ptr_array_add(alloc_list, var); | |
296 | var->block_flags = AML_NO_OPCODE; | |
297 | var->buf = build_alloc_array(); | |
298 | return var; | |
299 | } | |
300 | ||
3c054bd5 IM |
301 | static Aml *aml_opcode(uint8_t op) |
302 | { | |
303 | Aml *var = aml_alloc(); | |
304 | ||
305 | var->op = op; | |
306 | var->block_flags = AML_OPCODE; | |
307 | return var; | |
308 | } | |
309 | ||
2ef7c27b IM |
310 | static Aml *aml_bundle(uint8_t op, AmlBlockFlags flags) |
311 | { | |
312 | Aml *var = aml_alloc(); | |
313 | ||
314 | var->op = op; | |
315 | var->block_flags = flags; | |
316 | return var; | |
317 | } | |
318 | ||
2e5feadb | 319 | static void aml_free(gpointer data, gpointer user_data) |
0f2707e4 IM |
320 | { |
321 | Aml *var = data; | |
322 | build_free_array(var->buf); | |
afcf905c | 323 | g_free(var); |
0f2707e4 IM |
324 | } |
325 | ||
326 | Aml *init_aml_allocator(void) | |
327 | { | |
0f2707e4 | 328 | assert(!alloc_list); |
2e5feadb | 329 | alloc_list = g_ptr_array_new(); |
9be38598 | 330 | return aml_alloc(); |
0f2707e4 IM |
331 | } |
332 | ||
333 | void free_aml_allocator(void) | |
334 | { | |
2e5feadb | 335 | g_ptr_array_foreach(alloc_list, aml_free, NULL); |
0f2707e4 IM |
336 | g_ptr_array_free(alloc_list, true); |
337 | alloc_list = 0; | |
338 | } | |
339 | ||
340 | /* pack data with DefBuffer encoding */ | |
341 | static void build_buffer(GArray *array, uint8_t op) | |
342 | { | |
343 | GArray *data = build_alloc_array(); | |
344 | ||
345 | build_append_int(data, array->len); | |
346 | g_array_prepend_vals(array, data->data, data->len); | |
347 | build_free_array(data); | |
348 | build_package(array, op); | |
349 | } | |
350 | ||
351 | void aml_append(Aml *parent_ctx, Aml *child) | |
352 | { | |
7d433b0d MT |
353 | GArray *buf = build_alloc_array(); |
354 | build_append_array(buf, child->buf); | |
355 | ||
0f2707e4 IM |
356 | switch (child->block_flags) { |
357 | case AML_OPCODE: | |
358 | build_append_byte(parent_ctx->buf, child->op); | |
359 | break; | |
360 | case AML_EXT_PACKAGE: | |
7d433b0d | 361 | build_extop_package(buf, child->op); |
0f2707e4 IM |
362 | break; |
363 | case AML_PACKAGE: | |
7d433b0d | 364 | build_package(buf, child->op); |
0f2707e4 IM |
365 | break; |
366 | case AML_RES_TEMPLATE: | |
7d433b0d | 367 | build_append_byte(buf, 0x79); /* EndTag */ |
0f2707e4 IM |
368 | /* |
369 | * checksum operations are treated as succeeded if checksum | |
370 | * field is zero. [ACPI Spec 1.0b, 6.4.2.8 End Tag] | |
371 | */ | |
7d433b0d | 372 | build_append_byte(buf, 0); |
0f2707e4 IM |
373 | /* fall through, to pack resources in buffer */ |
374 | case AML_BUFFER: | |
7d433b0d | 375 | build_buffer(buf, child->op); |
0f2707e4 IM |
376 | break; |
377 | case AML_NO_OPCODE: | |
378 | break; | |
379 | default: | |
380 | assert(0); | |
381 | break; | |
382 | } | |
7d433b0d MT |
383 | build_append_array(parent_ctx->buf, buf); |
384 | build_free_array(buf); | |
0f2707e4 | 385 | } |
2ef7c27b IM |
386 | |
387 | /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefScope */ | |
388 | Aml *aml_scope(const char *name_format, ...) | |
389 | { | |
390 | va_list ap; | |
391 | Aml *var = aml_bundle(0x10 /* ScopeOp */, AML_PACKAGE); | |
392 | va_start(ap, name_format); | |
393 | build_append_namestringv(var->buf, name_format, ap); | |
394 | va_end(ap); | |
395 | return var; | |
396 | } | |
be06ebd0 | 397 | |
b25af5ad IM |
398 | /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefReturn */ |
399 | Aml *aml_return(Aml *val) | |
400 | { | |
401 | Aml *var = aml_opcode(0xA4 /* ReturnOp */); | |
402 | aml_append(var, val); | |
403 | return var; | |
404 | } | |
405 | ||
e8977414 IM |
406 | /* ACPI 1.0b: 16.2.6.3 Debug Objects Encoding: DebugObj */ |
407 | Aml *aml_debug(void) | |
408 | { | |
409 | Aml *var = aml_alloc(); | |
410 | build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */ | |
411 | build_append_byte(var->buf, 0x31); /* DebugOp */ | |
412 | return var; | |
413 | } | |
414 | ||
295a515d IM |
415 | /* |
416 | * ACPI 1.0b: 16.2.3 Data Objects Encoding: | |
417 | * encodes: ByteConst, WordConst, DWordConst, QWordConst, ZeroOp, OneOp | |
418 | */ | |
419 | Aml *aml_int(const uint64_t val) | |
420 | { | |
421 | Aml *var = aml_alloc(); | |
422 | build_append_int(var->buf, val); | |
423 | return var; | |
424 | } | |
425 | ||
3c054bd5 IM |
426 | /* |
427 | * helper to construct NameString, which returns Aml object | |
428 | * for using with aml_append or other aml_* terms | |
429 | */ | |
430 | Aml *aml_name(const char *name_format, ...) | |
431 | { | |
432 | va_list ap; | |
433 | Aml *var = aml_alloc(); | |
434 | va_start(ap, name_format); | |
435 | build_append_namestringv(var->buf, name_format, ap); | |
436 | va_end(ap); | |
437 | return var; | |
438 | } | |
439 | ||
440 | /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefName */ | |
441 | Aml *aml_name_decl(const char *name, Aml *val) | |
442 | { | |
443 | Aml *var = aml_opcode(0x08 /* NameOp */); | |
444 | build_append_namestring(var->buf, "%s", name); | |
445 | aml_append(var, val); | |
446 | return var; | |
447 | } | |
448 | ||
7193f3a6 IM |
449 | /* ACPI 1.0b: 16.2.6.1 Arg Objects Encoding */ |
450 | Aml *aml_arg(int pos) | |
451 | { | |
7193f3a6 IM |
452 | uint8_t op = 0x68 /* ARG0 op */ + pos; |
453 | ||
454 | assert(pos <= 6); | |
9be38598 | 455 | return aml_opcode(op); |
7193f3a6 IM |
456 | } |
457 | ||
f411199d IM |
458 | /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToInteger */ |
459 | Aml *aml_to_integer(Aml *arg) | |
460 | { | |
461 | Aml *var = aml_opcode(0x99 /* ToIntegerOp */); | |
462 | aml_append(var, arg); | |
463 | build_append_byte(var->buf, 0x00 /* NullNameOp */); | |
464 | return var; | |
465 | } | |
466 | ||
6d5ea945 IM |
467 | /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToHexString */ |
468 | Aml *aml_to_hexstring(Aml *src, Aml *dst) | |
469 | { | |
470 | Aml *var = aml_opcode(0x98 /* ToHexStringOp */); | |
471 | aml_append(var, src); | |
472 | if (dst) { | |
473 | aml_append(var, dst); | |
474 | } else { | |
475 | build_append_byte(var->buf, 0x00 /* NullNameOp */); | |
476 | } | |
477 | return var; | |
478 | } | |
479 | ||
25c1432e IM |
480 | /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToBuffer */ |
481 | Aml *aml_to_buffer(Aml *src, Aml *dst) | |
482 | { | |
483 | Aml *var = aml_opcode(0x96 /* ToBufferOp */); | |
484 | aml_append(var, src); | |
485 | if (dst) { | |
486 | aml_append(var, dst); | |
487 | } else { | |
488 | build_append_byte(var->buf, 0x00 /* NullNameOp */); | |
489 | } | |
490 | return var; | |
491 | } | |
492 | ||
c263b3f7 IM |
493 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefStore */ |
494 | Aml *aml_store(Aml *val, Aml *target) | |
495 | { | |
496 | Aml *var = aml_opcode(0x70 /* StoreOp */); | |
497 | aml_append(var, val); | |
498 | aml_append(var, target); | |
499 | return var; | |
500 | } | |
501 | ||
439e2a6e IM |
502 | /** |
503 | * build_opcode_2arg_dst: | |
504 | * @op: 1-byte opcode | |
505 | * @arg1: 1st operand | |
506 | * @arg2: 2nd operand | |
507 | * @dst: optional target to store to, set to NULL if it's not required | |
508 | * | |
509 | * An internal helper to compose AML terms that have | |
510 | * "Op Operand Operand Target" | |
511 | * pattern. | |
512 | * | |
513 | * Returns: The newly allocated and composed according to patter Aml object. | |
514 | */ | |
515 | static Aml * | |
516 | build_opcode_2arg_dst(uint8_t op, Aml *arg1, Aml *arg2, Aml *dst) | |
926f5aae | 517 | { |
439e2a6e | 518 | Aml *var = aml_opcode(op); |
926f5aae IM |
519 | aml_append(var, arg1); |
520 | aml_append(var, arg2); | |
439e2a6e IM |
521 | if (dst) { |
522 | aml_append(var, dst); | |
523 | } else { | |
524 | build_append_byte(var->buf, 0x00 /* NullNameOp */); | |
525 | } | |
926f5aae IM |
526 | return var; |
527 | } | |
528 | ||
439e2a6e | 529 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAnd */ |
5530427f | 530 | Aml *aml_and(Aml *arg1, Aml *arg2, Aml *dst) |
439e2a6e | 531 | { |
5530427f | 532 | return build_opcode_2arg_dst(0x7B /* AndOp */, arg1, arg2, dst); |
439e2a6e IM |
533 | } |
534 | ||
922cc882 | 535 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefOr */ |
ca3df95d | 536 | Aml *aml_or(Aml *arg1, Aml *arg2, Aml *dst) |
922cc882 | 537 | { |
ca3df95d | 538 | return build_opcode_2arg_dst(0x7D /* OrOp */, arg1, arg2, dst); |
922cc882 SZ |
539 | } |
540 | ||
df241999 IM |
541 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLOr */ |
542 | Aml *aml_lor(Aml *arg1, Aml *arg2) | |
543 | { | |
544 | Aml *var = aml_opcode(0x91 /* LOrOp */); | |
545 | aml_append(var, arg1); | |
546 | aml_append(var, arg2); | |
547 | return var; | |
548 | } | |
549 | ||
a57ddddd MA |
550 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftLeft */ |
551 | Aml *aml_shiftleft(Aml *arg1, Aml *count) | |
552 | { | |
439e2a6e | 553 | return build_opcode_2arg_dst(0x79 /* ShiftLeftOp */, arg1, count, NULL); |
a57ddddd MA |
554 | } |
555 | ||
f7bd7b8e | 556 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftRight */ |
c360639a | 557 | Aml *aml_shiftright(Aml *arg1, Aml *count, Aml *dst) |
f7bd7b8e | 558 | { |
c360639a | 559 | return build_opcode_2arg_dst(0x7A /* ShiftRightOp */, arg1, count, dst); |
f7bd7b8e MA |
560 | } |
561 | ||
96396e28 MA |
562 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLLess */ |
563 | Aml *aml_lless(Aml *arg1, Aml *arg2) | |
564 | { | |
565 | Aml *var = aml_opcode(0x95 /* LLessOp */); | |
566 | aml_append(var, arg1); | |
567 | aml_append(var, arg2); | |
568 | return var; | |
569 | } | |
570 | ||
c08cf070 | 571 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAdd */ |
20ca5208 | 572 | Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst) |
c08cf070 | 573 | { |
20ca5208 | 574 | return build_opcode_2arg_dst(0x72 /* AddOp */, arg1, arg2, dst); |
c08cf070 MA |
575 | } |
576 | ||
7059eb42 IM |
577 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSubtract */ |
578 | Aml *aml_subtract(Aml *arg1, Aml *arg2, Aml *dst) | |
579 | { | |
580 | return build_opcode_2arg_dst(0x74 /* SubtractOp */, arg1, arg2, dst); | |
581 | } | |
582 | ||
af39d536 MA |
583 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIncrement */ |
584 | Aml *aml_increment(Aml *arg) | |
585 | { | |
586 | Aml *var = aml_opcode(0x75 /* IncrementOp */); | |
587 | aml_append(var, arg); | |
588 | return var; | |
589 | } | |
590 | ||
7059eb42 IM |
591 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDecrement */ |
592 | Aml *aml_decrement(Aml *arg) | |
593 | { | |
594 | Aml *var = aml_opcode(0x76 /* DecrementOp */); | |
595 | aml_append(var, arg); | |
596 | return var; | |
597 | } | |
598 | ||
928b8996 MA |
599 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIndex */ |
600 | Aml *aml_index(Aml *arg1, Aml *idx) | |
601 | { | |
439e2a6e | 602 | return build_opcode_2arg_dst(0x88 /* IndexOp */, arg1, idx, NULL); |
928b8996 MA |
603 | } |
604 | ||
34189453 IM |
605 | /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefNotify */ |
606 | Aml *aml_notify(Aml *arg1, Aml *arg2) | |
607 | { | |
608 | Aml *var = aml_opcode(0x86 /* NotifyOp */); | |
609 | aml_append(var, arg1); | |
610 | aml_append(var, arg2); | |
611 | return var; | |
7b38ba9c IM |
612 | } |
613 | ||
614 | /* helper to call method with 1 argument */ | |
615 | Aml *aml_call0(const char *method) | |
616 | { | |
617 | Aml *var = aml_alloc(); | |
618 | build_append_namestring(var->buf, "%s", method); | |
619 | return var; | |
34189453 IM |
620 | } |
621 | ||
3f3992b7 IM |
622 | /* helper to call method with 1 argument */ |
623 | Aml *aml_call1(const char *method, Aml *arg1) | |
624 | { | |
625 | Aml *var = aml_alloc(); | |
626 | build_append_namestring(var->buf, "%s", method); | |
627 | aml_append(var, arg1); | |
628 | return var; | |
629 | } | |
630 | ||
631 | /* helper to call method with 2 arguments */ | |
632 | Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2) | |
633 | { | |
634 | Aml *var = aml_alloc(); | |
635 | build_append_namestring(var->buf, "%s", method); | |
636 | aml_append(var, arg1); | |
637 | aml_append(var, arg2); | |
638 | return var; | |
639 | } | |
640 | ||
641 | /* helper to call method with 3 arguments */ | |
642 | Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3) | |
643 | { | |
644 | Aml *var = aml_alloc(); | |
645 | build_append_namestring(var->buf, "%s", method); | |
646 | aml_append(var, arg1); | |
647 | aml_append(var, arg2); | |
648 | aml_append(var, arg3); | |
649 | return var; | |
650 | } | |
651 | ||
652 | /* helper to call method with 4 arguments */ | |
653 | Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4) | |
654 | { | |
655 | Aml *var = aml_alloc(); | |
656 | build_append_namestring(var->buf, "%s", method); | |
657 | aml_append(var, arg1); | |
658 | aml_append(var, arg2); | |
659 | aml_append(var, arg3); | |
660 | aml_append(var, arg4); | |
661 | return var; | |
662 | } | |
663 | ||
052889b8 XG |
664 | /* helper to call method with 5 arguments */ |
665 | Aml *aml_call5(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4, | |
666 | Aml *arg5) | |
667 | { | |
668 | Aml *var = aml_alloc(); | |
669 | build_append_namestring(var->buf, "%s", method); | |
670 | aml_append(var, arg1); | |
671 | aml_append(var, arg2); | |
672 | aml_append(var, arg3); | |
673 | aml_append(var, arg4); | |
674 | aml_append(var, arg5); | |
675 | return var; | |
676 | } | |
677 | ||
4ecdc746 SZ |
678 | /* |
679 | * ACPI 5.0: 6.4.3.8.1 GPIO Connection Descriptor | |
680 | * Type 1, Large Item Name 0xC | |
681 | */ | |
682 | ||
683 | static Aml *aml_gpio_connection(AmlGpioConnectionType type, | |
684 | AmlConsumerAndProducer con_and_pro, | |
685 | uint8_t flags, AmlPinConfig pin_config, | |
686 | uint16_t output_drive, | |
687 | uint16_t debounce_timeout, | |
688 | const uint32_t pin_list[], uint32_t pin_count, | |
689 | const char *resource_source_name, | |
690 | const uint8_t *vendor_data, | |
691 | uint16_t vendor_data_len) | |
692 | { | |
693 | Aml *var = aml_alloc(); | |
694 | const uint16_t min_desc_len = 0x16; | |
695 | uint16_t resource_source_name_len, length; | |
696 | uint16_t pin_table_offset, resource_source_name_offset, vendor_data_offset; | |
697 | uint32_t i; | |
698 | ||
699 | assert(resource_source_name); | |
700 | resource_source_name_len = strlen(resource_source_name) + 1; | |
701 | length = min_desc_len + resource_source_name_len + vendor_data_len; | |
702 | pin_table_offset = min_desc_len + 1; | |
703 | resource_source_name_offset = pin_table_offset + pin_count * 2; | |
704 | vendor_data_offset = resource_source_name_offset + resource_source_name_len; | |
705 | ||
706 | build_append_byte(var->buf, 0x8C); /* GPIO Connection Descriptor */ | |
707 | build_append_int_noprefix(var->buf, length, 2); /* Length */ | |
708 | build_append_byte(var->buf, 1); /* Revision ID */ | |
709 | build_append_byte(var->buf, type); /* GPIO Connection Type */ | |
710 | /* General Flags (2 bytes) */ | |
711 | build_append_int_noprefix(var->buf, con_and_pro, 2); | |
712 | /* Interrupt and IO Flags (2 bytes) */ | |
713 | build_append_int_noprefix(var->buf, flags, 2); | |
714 | /* Pin Configuration 0 = Default 1 = Pull-up 2 = Pull-down 3 = No Pull */ | |
715 | build_append_byte(var->buf, pin_config); | |
716 | /* Output Drive Strength (2 bytes) */ | |
717 | build_append_int_noprefix(var->buf, output_drive, 2); | |
718 | /* Debounce Timeout (2 bytes) */ | |
719 | build_append_int_noprefix(var->buf, debounce_timeout, 2); | |
720 | /* Pin Table Offset (2 bytes) */ | |
721 | build_append_int_noprefix(var->buf, pin_table_offset, 2); | |
722 | build_append_byte(var->buf, 0); /* Resource Source Index */ | |
723 | /* Resource Source Name Offset (2 bytes) */ | |
724 | build_append_int_noprefix(var->buf, resource_source_name_offset, 2); | |
725 | /* Vendor Data Offset (2 bytes) */ | |
726 | build_append_int_noprefix(var->buf, vendor_data_offset, 2); | |
727 | /* Vendor Data Length (2 bytes) */ | |
728 | build_append_int_noprefix(var->buf, vendor_data_len, 2); | |
729 | /* Pin Number (2n bytes)*/ | |
730 | for (i = 0; i < pin_count; i++) { | |
731 | build_append_int_noprefix(var->buf, pin_list[i], 2); | |
732 | } | |
733 | ||
734 | /* Resource Source Name */ | |
735 | build_append_namestring(var->buf, "%s", resource_source_name); | |
736 | build_append_byte(var->buf, '\0'); | |
737 | ||
738 | /* Vendor-defined Data */ | |
739 | if (vendor_data != NULL) { | |
740 | g_array_append_vals(var->buf, vendor_data, vendor_data_len); | |
741 | } | |
742 | ||
743 | return var; | |
744 | } | |
745 | ||
37d0e980 SZ |
746 | /* |
747 | * ACPI 5.0: 19.5.53 | |
748 | * GpioInt(GPIO Interrupt Connection Resource Descriptor Macro) | |
749 | */ | |
750 | Aml *aml_gpio_int(AmlConsumerAndProducer con_and_pro, | |
751 | AmlLevelAndEdge edge_level, | |
752 | AmlActiveHighAndLow active_level, AmlShared shared, | |
753 | AmlPinConfig pin_config, uint16_t debounce_timeout, | |
754 | const uint32_t pin_list[], uint32_t pin_count, | |
755 | const char *resource_source_name, | |
756 | const uint8_t *vendor_data, uint16_t vendor_data_len) | |
757 | { | |
758 | uint8_t flags = edge_level | (active_level << 1) | (shared << 3); | |
759 | ||
760 | return aml_gpio_connection(AML_INTERRUPT_CONNECTION, con_and_pro, flags, | |
761 | pin_config, 0, debounce_timeout, pin_list, | |
762 | pin_count, resource_source_name, vendor_data, | |
763 | vendor_data_len); | |
764 | } | |
765 | ||
dc17ab1d SZ |
766 | /* |
767 | * ACPI 1.0b: 6.4.3.4 32-Bit Fixed Location Memory Range Descriptor | |
768 | * (Type 1, Large Item Name 0x6) | |
769 | */ | |
770 | Aml *aml_memory32_fixed(uint32_t addr, uint32_t size, | |
771 | AmlReadAndWrite read_and_write) | |
772 | { | |
773 | Aml *var = aml_alloc(); | |
774 | build_append_byte(var->buf, 0x86); /* Memory32Fixed Resource Descriptor */ | |
775 | build_append_byte(var->buf, 9); /* Length, bits[7:0] value = 9 */ | |
776 | build_append_byte(var->buf, 0); /* Length, bits[15:8] value = 0 */ | |
777 | build_append_byte(var->buf, read_and_write); /* Write status, 1 rw 0 ro */ | |
778 | ||
779 | /* Range base address */ | |
780 | build_append_byte(var->buf, extract32(addr, 0, 8)); /* bits[7:0] */ | |
781 | build_append_byte(var->buf, extract32(addr, 8, 8)); /* bits[15:8] */ | |
782 | build_append_byte(var->buf, extract32(addr, 16, 8)); /* bits[23:16] */ | |
783 | build_append_byte(var->buf, extract32(addr, 24, 8)); /* bits[31:24] */ | |
784 | ||
785 | /* Range length */ | |
786 | build_append_byte(var->buf, extract32(size, 0, 8)); /* bits[7:0] */ | |
787 | build_append_byte(var->buf, extract32(size, 8, 8)); /* bits[15:8] */ | |
788 | build_append_byte(var->buf, extract32(size, 16, 8)); /* bits[23:16] */ | |
789 | build_append_byte(var->buf, extract32(size, 24, 8)); /* bits[31:24] */ | |
790 | return var; | |
791 | } | |
792 | ||
205d1d1c SZ |
793 | /* |
794 | * ACPI 5.0: 6.4.3.6 Extended Interrupt Descriptor | |
795 | * Type 1, Large Item Name 0x9 | |
796 | */ | |
797 | Aml *aml_interrupt(AmlConsumerAndProducer con_and_pro, | |
798 | AmlLevelAndEdge level_and_edge, | |
799 | AmlActiveHighAndLow high_and_low, AmlShared shared, | |
45fcf539 | 800 | uint32_t *irq_list, uint8_t irq_count) |
205d1d1c | 801 | { |
45fcf539 | 802 | int i; |
205d1d1c SZ |
803 | Aml *var = aml_alloc(); |
804 | uint8_t irq_flags = con_and_pro | (level_and_edge << 1) | |
805 | | (high_and_low << 2) | (shared << 3); | |
45fcf539 IM |
806 | const int header_bytes_in_len = 2; |
807 | uint16_t len = header_bytes_in_len + irq_count * sizeof(uint32_t); | |
808 | ||
809 | assert(irq_count > 0); | |
205d1d1c SZ |
810 | |
811 | build_append_byte(var->buf, 0x89); /* Extended irq descriptor */ | |
45fcf539 IM |
812 | build_append_byte(var->buf, len & 0xFF); /* Length, bits[7:0] */ |
813 | build_append_byte(var->buf, len >> 8); /* Length, bits[15:8] */ | |
205d1d1c | 814 | build_append_byte(var->buf, irq_flags); /* Interrupt Vector Information. */ |
45fcf539 | 815 | build_append_byte(var->buf, irq_count); /* Interrupt table length */ |
205d1d1c | 816 | |
45fcf539 IM |
817 | /* Interrupt Number List */ |
818 | for (i = 0; i < irq_count; i++) { | |
819 | build_append_int_noprefix(var->buf, irq_list[i], 4); | |
820 | } | |
205d1d1c SZ |
821 | return var; |
822 | } | |
823 | ||
52fa397c IM |
824 | /* ACPI 1.0b: 6.4.2.5 I/O Port Descriptor */ |
825 | Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, | |
826 | uint8_t aln, uint8_t len) | |
827 | { | |
828 | Aml *var = aml_alloc(); | |
829 | build_append_byte(var->buf, 0x47); /* IO port descriptor */ | |
830 | build_append_byte(var->buf, dec); | |
831 | build_append_byte(var->buf, min_base & 0xff); | |
832 | build_append_byte(var->buf, (min_base >> 8) & 0xff); | |
833 | build_append_byte(var->buf, max_base & 0xff); | |
834 | build_append_byte(var->buf, (max_base >> 8) & 0xff); | |
835 | build_append_byte(var->buf, aln); | |
836 | build_append_byte(var->buf, len); | |
837 | return var; | |
838 | } | |
839 | ||
70560453 IM |
840 | /* |
841 | * ACPI 1.0b: 6.4.2.1.1 ASL Macro for IRQ Descriptor | |
842 | * | |
843 | * More verbose description at: | |
844 | * ACPI 5.0: 19.5.64 IRQNoFlags (Interrupt Resource Descriptor Macro) | |
845 | * 6.4.2.1 IRQ Descriptor | |
846 | */ | |
847 | Aml *aml_irq_no_flags(uint8_t irq) | |
848 | { | |
849 | uint16_t irq_mask; | |
850 | Aml *var = aml_alloc(); | |
851 | ||
852 | assert(irq < 16); | |
853 | build_append_byte(var->buf, 0x22); /* IRQ descriptor 2 byte form */ | |
854 | ||
855 | irq_mask = 1U << irq; | |
856 | build_append_byte(var->buf, irq_mask & 0xFF); /* IRQ mask bits[7:0] */ | |
857 | build_append_byte(var->buf, irq_mask >> 8); /* IRQ mask bits[15:8] */ | |
858 | return var; | |
859 | } | |
860 | ||
ea7df04a SZ |
861 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLNot */ |
862 | Aml *aml_lnot(Aml *arg) | |
863 | { | |
864 | Aml *var = aml_opcode(0x92 /* LNotOp */); | |
865 | aml_append(var, arg); | |
866 | return var; | |
867 | } | |
868 | ||
15e44e56 IM |
869 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLEqual */ |
870 | Aml *aml_equal(Aml *arg1, Aml *arg2) | |
871 | { | |
872 | Aml *var = aml_opcode(0x93 /* LequalOp */); | |
873 | aml_append(var, arg1); | |
dabad78b IM |
874 | aml_append(var, arg2); |
875 | return var; | |
876 | } | |
877 | ||
878 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLGreater */ | |
879 | Aml *aml_lgreater(Aml *arg1, Aml *arg2) | |
880 | { | |
881 | Aml *var = aml_opcode(0x94 /* LGreaterOp */); | |
882 | aml_append(var, arg1); | |
15e44e56 | 883 | aml_append(var, arg2); |
15e44e56 IM |
884 | return var; |
885 | } | |
886 | ||
2d3f667d IM |
887 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLGreaterEqual */ |
888 | Aml *aml_lgreater_equal(Aml *arg1, Aml *arg2) | |
889 | { | |
890 | /* LGreaterEqualOp := LNotOp LLessOp */ | |
891 | Aml *var = aml_opcode(0x92 /* LNotOp */); | |
892 | build_append_byte(var->buf, 0x95 /* LLessOp */); | |
893 | aml_append(var, arg1); | |
894 | aml_append(var, arg2); | |
895 | return var; | |
896 | } | |
897 | ||
32acac9e IM |
898 | /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */ |
899 | Aml *aml_if(Aml *predicate) | |
900 | { | |
901 | Aml *var = aml_bundle(0xA0 /* IfOp */, AML_PACKAGE); | |
902 | aml_append(var, predicate); | |
903 | return var; | |
904 | } | |
905 | ||
467b07df SZ |
906 | /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefElse */ |
907 | Aml *aml_else(void) | |
908 | { | |
909 | Aml *var = aml_bundle(0xA1 /* ElseOp */, AML_PACKAGE); | |
910 | return var; | |
911 | } | |
912 | ||
32d9ca15 MA |
913 | /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefWhile */ |
914 | Aml *aml_while(Aml *predicate) | |
915 | { | |
916 | Aml *var = aml_bundle(0xA2 /* WhileOp */, AML_PACKAGE); | |
917 | aml_append(var, predicate); | |
918 | return var; | |
919 | } | |
920 | ||
ea2407d7 | 921 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */ |
4dbfc881 | 922 | Aml *aml_method(const char *name, int arg_count, AmlSerializeFlag sflag) |
ea2407d7 IM |
923 | { |
924 | Aml *var = aml_bundle(0x14 /* MethodOp */, AML_PACKAGE); | |
4dbfc881 XG |
925 | int methodflags; |
926 | ||
927 | /* | |
928 | * MethodFlags: | |
929 | * bit 0-2: ArgCount (0-7) | |
930 | * bit 3: SerializeFlag | |
931 | * 0: NotSerialized | |
932 | * 1: Serialized | |
933 | * bit 4-7: reserved (must be 0) | |
934 | */ | |
935 | assert(arg_count < 8); | |
936 | methodflags = arg_count | (sflag << 3); | |
937 | ||
ea2407d7 | 938 | build_append_namestring(var->buf, "%s", name); |
4dbfc881 | 939 | build_append_byte(var->buf, methodflags); /* MethodFlags: ArgCount */ |
ea2407d7 IM |
940 | return var; |
941 | } | |
942 | ||
be06ebd0 IM |
943 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */ |
944 | Aml *aml_device(const char *name_format, ...) | |
945 | { | |
946 | va_list ap; | |
947 | Aml *var = aml_bundle(0x82 /* DeviceOp */, AML_EXT_PACKAGE); | |
948 | va_start(ap, name_format); | |
949 | build_append_namestringv(var->buf, name_format, ap); | |
950 | va_end(ap); | |
951 | return var; | |
952 | } | |
3bfa74a7 | 953 | |
ad4a80bc IM |
954 | /* ACPI 1.0b: 6.4.1 ASL Macros for Resource Descriptors */ |
955 | Aml *aml_resource_template(void) | |
956 | { | |
957 | /* ResourceTemplate is a buffer of Resources with EndTag at the end */ | |
958 | Aml *var = aml_bundle(0x11 /* BufferOp */, AML_RES_TEMPLATE); | |
959 | return var; | |
960 | } | |
961 | ||
ed8b5847 SZ |
962 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefBuffer |
963 | * Pass byte_list as NULL to request uninitialized buffer to reserve space. | |
964 | */ | |
965 | Aml *aml_buffer(int buffer_size, uint8_t *byte_list) | |
04b8da54 | 966 | { |
ed8b5847 | 967 | int i; |
04b8da54 | 968 | Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER); |
ed8b5847 SZ |
969 | |
970 | for (i = 0; i < buffer_size; i++) { | |
971 | if (byte_list == NULL) { | |
972 | build_append_byte(var->buf, 0x0); | |
973 | } else { | |
974 | build_append_byte(var->buf, byte_list[i]); | |
975 | } | |
976 | } | |
977 | ||
04b8da54 IM |
978 | return var; |
979 | } | |
980 | ||
3bfa74a7 IM |
981 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefPackage */ |
982 | Aml *aml_package(uint8_t num_elements) | |
983 | { | |
984 | Aml *var = aml_bundle(0x12 /* PackageOp */, AML_PACKAGE); | |
985 | build_append_byte(var->buf, num_elements); | |
986 | return var; | |
987 | } | |
31127938 IM |
988 | |
989 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefOpRegion */ | |
990 | Aml *aml_operation_region(const char *name, AmlRegionSpace rs, | |
3f3009c0 | 991 | Aml *offset, uint32_t len) |
31127938 IM |
992 | { |
993 | Aml *var = aml_alloc(); | |
994 | build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */ | |
995 | build_append_byte(var->buf, 0x80); /* OpRegionOp */ | |
996 | build_append_namestring(var->buf, "%s", name); | |
997 | build_append_byte(var->buf, rs); | |
3f3009c0 | 998 | aml_append(var, offset); |
31127938 IM |
999 | build_append_int(var->buf, len); |
1000 | return var; | |
1001 | } | |
214ae59f IM |
1002 | |
1003 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: NamedField */ | |
1004 | Aml *aml_named_field(const char *name, unsigned length) | |
1005 | { | |
1006 | Aml *var = aml_alloc(); | |
1007 | build_append_nameseg(var->buf, name); | |
1008 | build_append_pkg_length(var->buf, length, false); | |
1009 | return var; | |
1010 | } | |
1011 | ||
e2ea299b IM |
1012 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: ReservedField */ |
1013 | Aml *aml_reserved_field(unsigned length) | |
1014 | { | |
1015 | Aml *var = aml_alloc(); | |
1016 | /* ReservedField := 0x00 PkgLength */ | |
1017 | build_append_byte(var->buf, 0x00); | |
1018 | build_append_pkg_length(var->buf, length, false); | |
1019 | return var; | |
1020 | } | |
1021 | ||
214ae59f | 1022 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefField */ |
36de884a IM |
1023 | Aml *aml_field(const char *name, AmlAccessType type, AmlLockRule lock, |
1024 | AmlUpdateRule rule) | |
214ae59f IM |
1025 | { |
1026 | Aml *var = aml_bundle(0x81 /* FieldOp */, AML_EXT_PACKAGE); | |
af509897 ZG |
1027 | uint8_t flags = rule << 5 | type; |
1028 | ||
36de884a IM |
1029 | flags |= lock << 4; /* LockRule at 4 bit offset */ |
1030 | ||
214ae59f IM |
1031 | build_append_namestring(var->buf, "%s", name); |
1032 | build_append_byte(var->buf, flags); | |
1033 | return var; | |
1034 | } | |
b8a5d689 | 1035 | |
7e192a38 IM |
1036 | static |
1037 | Aml *create_field_common(int opcode, Aml *srcbuf, Aml *index, const char *name) | |
ed8176a3 | 1038 | { |
7e192a38 | 1039 | Aml *var = aml_opcode(opcode); |
ed8176a3 SZ |
1040 | aml_append(var, srcbuf); |
1041 | aml_append(var, index); | |
1042 | build_append_namestring(var->buf, "%s", name); | |
1043 | return var; | |
1044 | } | |
1045 | ||
39b6dbd8 XG |
1046 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefCreateField */ |
1047 | Aml *aml_create_field(Aml *srcbuf, Aml *bit_index, Aml *num_bits, | |
1048 | const char *name) | |
1049 | { | |
1050 | Aml *var = aml_alloc(); | |
1051 | build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */ | |
1052 | build_append_byte(var->buf, 0x13); /* CreateFieldOp */ | |
1053 | aml_append(var, srcbuf); | |
1054 | aml_append(var, bit_index); | |
1055 | aml_append(var, num_bits); | |
1056 | build_append_namestring(var->buf, "%s", name); | |
1057 | return var; | |
1058 | } | |
1059 | ||
7e192a38 IM |
1060 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefCreateDWordField */ |
1061 | Aml *aml_create_dword_field(Aml *srcbuf, Aml *index, const char *name) | |
1062 | { | |
1063 | return create_field_common(0x8A /* CreateDWordFieldOp */, | |
1064 | srcbuf, index, name); | |
1065 | } | |
1066 | ||
1067 | /* ACPI 2.0a: 17.2.4.2 Named Objects Encoding: DefCreateQWordField */ | |
1068 | Aml *aml_create_qword_field(Aml *srcbuf, Aml *index, const char *name) | |
1069 | { | |
1070 | return create_field_common(0x8F /* CreateQWordFieldOp */, | |
1071 | srcbuf, index, name); | |
1072 | } | |
1073 | ||
d5e5830f IM |
1074 | /* ACPI 1.0b: 16.2.3 Data Objects Encoding: String */ |
1075 | Aml *aml_string(const char *name_format, ...) | |
1076 | { | |
1077 | Aml *var = aml_opcode(0x0D /* StringPrefix */); | |
c3bdc56c | 1078 | va_list ap; |
d5e5830f IM |
1079 | char *s; |
1080 | int len; | |
1081 | ||
1082 | va_start(ap, name_format); | |
c3bdc56c | 1083 | len = g_vasprintf(&s, name_format, ap); |
d5e5830f IM |
1084 | va_end(ap); |
1085 | ||
c3bdc56c | 1086 | g_array_append_vals(var->buf, s, len + 1); |
d5e5830f IM |
1087 | g_free(s); |
1088 | ||
1089 | return var; | |
1090 | } | |
1091 | ||
b8a5d689 IM |
1092 | /* ACPI 1.0b: 16.2.6.2 Local Objects Encoding */ |
1093 | Aml *aml_local(int num) | |
1094 | { | |
b8a5d689 IM |
1095 | uint8_t op = 0x60 /* Local0Op */ + num; |
1096 | ||
1097 | assert(num <= 7); | |
9be38598 | 1098 | return aml_opcode(op); |
b8a5d689 | 1099 | } |
a678508e IM |
1100 | |
1101 | /* ACPI 2.0a: 17.2.2 Data Objects Encoding: DefVarPackage */ | |
1102 | Aml *aml_varpackage(uint32_t num_elements) | |
1103 | { | |
1104 | Aml *var = aml_bundle(0x13 /* VarPackageOp */, AML_PACKAGE); | |
1105 | build_append_int(var->buf, num_elements); | |
1106 | return var; | |
1107 | } | |
3dd15643 IM |
1108 | |
1109 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefProcessor */ | |
1110 | Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len, | |
1111 | const char *name_format, ...) | |
1112 | { | |
1113 | va_list ap; | |
1114 | Aml *var = aml_bundle(0x83 /* ProcessorOp */, AML_EXT_PACKAGE); | |
1115 | va_start(ap, name_format); | |
1116 | build_append_namestringv(var->buf, name_format, ap); | |
1117 | va_end(ap); | |
1118 | build_append_byte(var->buf, proc_id); /* ProcID */ | |
1119 | build_append_int_noprefix(var->buf, pblk_addr, sizeof(pblk_addr)); | |
1120 | build_append_byte(var->buf, pblk_len); /* PblkLen */ | |
1121 | return var; | |
1122 | } | |
a7891dac IM |
1123 | |
1124 | static uint8_t Hex2Digit(char c) | |
1125 | { | |
1126 | if (c >= 'A') { | |
1127 | return c - 'A' + 10; | |
1128 | } | |
1129 | ||
1130 | return c - '0'; | |
1131 | } | |
1132 | ||
1133 | /* ACPI 1.0b: 15.2.3.6.4.1 EISAID Macro - Convert EISA ID String To Integer */ | |
1134 | Aml *aml_eisaid(const char *str) | |
1135 | { | |
1136 | Aml *var = aml_alloc(); | |
1137 | uint32_t id; | |
1138 | ||
1139 | g_assert(strlen(str) == 7); | |
1140 | id = (str[0] - 0x40) << 26 | | |
1141 | (str[1] - 0x40) << 21 | | |
1142 | (str[2] - 0x40) << 16 | | |
1143 | Hex2Digit(str[3]) << 12 | | |
1144 | Hex2Digit(str[4]) << 8 | | |
1145 | Hex2Digit(str[5]) << 4 | | |
1146 | Hex2Digit(str[6]); | |
1147 | ||
1148 | build_append_byte(var->buf, 0x0C); /* DWordPrefix */ | |
1149 | build_append_int_noprefix(var->buf, bswap32(id), sizeof(id)); | |
1150 | return var; | |
1151 | } | |
6ece7053 IM |
1152 | |
1153 | /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor: bytes 3-5 */ | |
1154 | static Aml *aml_as_desc_header(AmlResourceType type, AmlMinFixed min_fixed, | |
1155 | AmlMaxFixed max_fixed, AmlDecode dec, | |
1156 | uint8_t type_flags) | |
1157 | { | |
1158 | uint8_t flags = max_fixed | min_fixed | dec; | |
1159 | Aml *var = aml_alloc(); | |
1160 | ||
1161 | build_append_byte(var->buf, type); | |
1162 | build_append_byte(var->buf, flags); | |
1163 | build_append_byte(var->buf, type_flags); /* Type Specific Flags */ | |
1164 | return var; | |
1165 | } | |
1166 | ||
1167 | /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor */ | |
1168 | static Aml *aml_word_as_desc(AmlResourceType type, AmlMinFixed min_fixed, | |
1169 | AmlMaxFixed max_fixed, AmlDecode dec, | |
1170 | uint16_t addr_gran, uint16_t addr_min, | |
1171 | uint16_t addr_max, uint16_t addr_trans, | |
1172 | uint16_t len, uint8_t type_flags) | |
1173 | { | |
1174 | Aml *var = aml_alloc(); | |
1175 | ||
1176 | build_append_byte(var->buf, 0x88); /* Word Address Space Descriptor */ | |
1177 | /* minimum length since we do not encode optional fields */ | |
1178 | build_append_byte(var->buf, 0x0D); | |
1179 | build_append_byte(var->buf, 0x0); | |
1180 | ||
1181 | aml_append(var, | |
1182 | aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags)); | |
1183 | build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran)); | |
1184 | build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min)); | |
1185 | build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max)); | |
1186 | build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans)); | |
1187 | build_append_int_noprefix(var->buf, len, sizeof(len)); | |
1188 | return var; | |
1189 | } | |
1190 | ||
1191 | /* ACPI 1.0b: 6.4.3.5.3 DWord Address Space Descriptor */ | |
1192 | static Aml *aml_dword_as_desc(AmlResourceType type, AmlMinFixed min_fixed, | |
1193 | AmlMaxFixed max_fixed, AmlDecode dec, | |
1194 | uint32_t addr_gran, uint32_t addr_min, | |
1195 | uint32_t addr_max, uint32_t addr_trans, | |
1196 | uint32_t len, uint8_t type_flags) | |
1197 | { | |
1198 | Aml *var = aml_alloc(); | |
1199 | ||
1200 | build_append_byte(var->buf, 0x87); /* DWord Address Space Descriptor */ | |
1201 | /* minimum length since we do not encode optional fields */ | |
1202 | build_append_byte(var->buf, 23); | |
1203 | build_append_byte(var->buf, 0x0); | |
1204 | ||
1205 | ||
1206 | aml_append(var, | |
1207 | aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags)); | |
1208 | build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran)); | |
1209 | build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min)); | |
1210 | build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max)); | |
1211 | build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans)); | |
1212 | build_append_int_noprefix(var->buf, len, sizeof(len)); | |
1213 | return var; | |
1214 | } | |
1215 | ||
1216 | /* ACPI 1.0b: 6.4.3.5.1 QWord Address Space Descriptor */ | |
1217 | static Aml *aml_qword_as_desc(AmlResourceType type, AmlMinFixed min_fixed, | |
1218 | AmlMaxFixed max_fixed, AmlDecode dec, | |
1219 | uint64_t addr_gran, uint64_t addr_min, | |
1220 | uint64_t addr_max, uint64_t addr_trans, | |
1221 | uint64_t len, uint8_t type_flags) | |
1222 | { | |
1223 | Aml *var = aml_alloc(); | |
1224 | ||
1225 | build_append_byte(var->buf, 0x8A); /* QWord Address Space Descriptor */ | |
1226 | /* minimum length since we do not encode optional fields */ | |
1227 | build_append_byte(var->buf, 0x2B); | |
1228 | build_append_byte(var->buf, 0x0); | |
1229 | ||
1230 | aml_append(var, | |
1231 | aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags)); | |
1232 | build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran)); | |
1233 | build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min)); | |
1234 | build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max)); | |
1235 | build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans)); | |
1236 | build_append_int_noprefix(var->buf, len, sizeof(len)); | |
1237 | return var; | |
1238 | } | |
1239 | ||
1240 | /* | |
1241 | * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor | |
1242 | * | |
1243 | * More verbose description at: | |
1244 | * ACPI 5.0: 19.5.141 WordBusNumber (Word Bus Number Resource Descriptor Macro) | |
1245 | */ | |
1246 | Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, | |
1247 | AmlDecode dec, uint16_t addr_gran, | |
1248 | uint16_t addr_min, uint16_t addr_max, | |
1249 | uint16_t addr_trans, uint16_t len) | |
1250 | ||
1251 | { | |
ff80dc7f | 1252 | return aml_word_as_desc(AML_BUS_NUMBER_RANGE, min_fixed, max_fixed, dec, |
6ece7053 IM |
1253 | addr_gran, addr_min, addr_max, addr_trans, len, 0); |
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.142 WordIO (Word IO Resource Descriptor Macro) | |
1261 | */ | |
1262 | Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, | |
1263 | AmlDecode dec, AmlISARanges isa_ranges, | |
1264 | uint16_t addr_gran, uint16_t addr_min, | |
1265 | uint16_t addr_max, uint16_t addr_trans, | |
1266 | uint16_t len) | |
1267 | ||
1268 | { | |
ff80dc7f | 1269 | return aml_word_as_desc(AML_IO_RANGE, min_fixed, max_fixed, dec, |
6ece7053 IM |
1270 | addr_gran, addr_min, addr_max, addr_trans, len, |
1271 | isa_ranges); | |
1272 | } | |
1273 | ||
616ef329 SZ |
1274 | /* |
1275 | * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Descriptor | |
1276 | * | |
1277 | * More verbose description at: | |
1278 | * ACPI 5.0: 19.5.33 DWordIO (DWord IO Resource Descriptor Macro) | |
1279 | */ | |
1280 | Aml *aml_dword_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, | |
1281 | AmlDecode dec, AmlISARanges isa_ranges, | |
1282 | uint32_t addr_gran, uint32_t addr_min, | |
1283 | uint32_t addr_max, uint32_t addr_trans, | |
1284 | uint32_t len) | |
1285 | ||
1286 | { | |
1287 | return aml_dword_as_desc(AML_IO_RANGE, min_fixed, max_fixed, dec, | |
1288 | addr_gran, addr_min, addr_max, addr_trans, len, | |
1289 | isa_ranges); | |
1290 | } | |
1291 | ||
6ece7053 IM |
1292 | /* |
1293 | * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Space Descriptor | |
1294 | * | |
1295 | * More verbose description at: | |
1296 | * ACPI 5.0: 19.5.34 DWordMemory (DWord Memory Resource Descriptor Macro) | |
1297 | */ | |
1298 | Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed, | |
ff80dc7f | 1299 | AmlMaxFixed max_fixed, AmlCacheable cacheable, |
6ece7053 IM |
1300 | AmlReadAndWrite read_and_write, |
1301 | uint32_t addr_gran, uint32_t addr_min, | |
1302 | uint32_t addr_max, uint32_t addr_trans, | |
1303 | uint32_t len) | |
1304 | { | |
1305 | uint8_t flags = read_and_write | (cacheable << 1); | |
1306 | ||
ff80dc7f | 1307 | return aml_dword_as_desc(AML_MEMORY_RANGE, min_fixed, max_fixed, |
6ece7053 IM |
1308 | dec, addr_gran, addr_min, addr_max, |
1309 | addr_trans, len, flags); | |
1310 | } | |
1311 | ||
1312 | /* | |
1313 | * ACPI 1.0b: 6.4.3.5.2 ASL Macros for QWORD Address Space Descriptor | |
1314 | * | |
1315 | * More verbose description at: | |
1316 | * ACPI 5.0: 19.5.102 QWordMemory (QWord Memory Resource Descriptor Macro) | |
1317 | */ | |
1318 | Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed, | |
ff80dc7f | 1319 | AmlMaxFixed max_fixed, AmlCacheable cacheable, |
6ece7053 IM |
1320 | AmlReadAndWrite read_and_write, |
1321 | uint64_t addr_gran, uint64_t addr_min, | |
1322 | uint64_t addr_max, uint64_t addr_trans, | |
1323 | uint64_t len) | |
1324 | { | |
1325 | uint8_t flags = read_and_write | (cacheable << 1); | |
1326 | ||
ff80dc7f | 1327 | return aml_qword_as_desc(AML_MEMORY_RANGE, min_fixed, max_fixed, |
6ece7053 IM |
1328 | dec, addr_gran, addr_min, addr_max, |
1329 | addr_trans, len, flags); | |
1330 | } | |
658c2718 | 1331 | |
a23b8872 IM |
1332 | /* ACPI 1.0b: 6.4.2.2 DMA Format/6.4.2.2.1 ASL Macro for DMA Descriptor */ |
1333 | Aml *aml_dma(AmlDmaType typ, AmlDmaBusMaster bm, AmlTransferSize sz, | |
1334 | uint8_t channel) | |
1335 | { | |
1336 | Aml *var = aml_alloc(); | |
1337 | uint8_t flags = sz | bm << 2 | typ << 5; | |
1338 | ||
1339 | assert(channel < 8); | |
1340 | build_append_byte(var->buf, 0x2A); /* Byte 0: DMA Descriptor */ | |
1341 | build_append_byte(var->buf, 1U << channel); /* Byte 1: _DMA - DmaChannel */ | |
1342 | build_append_byte(var->buf, flags); /* Byte 2 */ | |
1343 | return var; | |
1344 | } | |
1345 | ||
0073518d IM |
1346 | /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefSleep */ |
1347 | Aml *aml_sleep(uint64_t msec) | |
1348 | { | |
1349 | Aml *var = aml_alloc(); | |
1350 | build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */ | |
1351 | build_append_byte(var->buf, 0x22); /* SleepOp */ | |
1352 | aml_append(var, aml_int(msec)); | |
1353 | return var; | |
1354 | } | |
1355 | ||
b930fb9d SZ |
1356 | static uint8_t Hex2Byte(const char *src) |
1357 | { | |
1358 | int hi, lo; | |
1359 | ||
1360 | hi = Hex2Digit(src[0]); | |
1361 | assert(hi >= 0); | |
1362 | assert(hi <= 15); | |
1363 | ||
1364 | lo = Hex2Digit(src[1]); | |
1365 | assert(lo >= 0); | |
1366 | assert(lo <= 15); | |
1367 | return (hi << 4) | lo; | |
1368 | } | |
1369 | ||
1370 | /* | |
1371 | * ACPI 3.0: 17.5.124 ToUUID (Convert String to UUID Macro) | |
1372 | * e.g. UUID: aabbccdd-eeff-gghh-iijj-kkllmmnnoopp | |
1373 | * call aml_touuid("aabbccdd-eeff-gghh-iijj-kkllmmnnoopp"); | |
1374 | */ | |
1375 | Aml *aml_touuid(const char *uuid) | |
1376 | { | |
1377 | Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER); | |
1378 | ||
1379 | assert(strlen(uuid) == 36); | |
1380 | assert(uuid[8] == '-'); | |
1381 | assert(uuid[13] == '-'); | |
1382 | assert(uuid[18] == '-'); | |
1383 | assert(uuid[23] == '-'); | |
1384 | ||
1385 | build_append_byte(var->buf, Hex2Byte(uuid + 6)); /* dd - at offset 00 */ | |
1386 | build_append_byte(var->buf, Hex2Byte(uuid + 4)); /* cc - at offset 01 */ | |
1387 | build_append_byte(var->buf, Hex2Byte(uuid + 2)); /* bb - at offset 02 */ | |
1388 | build_append_byte(var->buf, Hex2Byte(uuid + 0)); /* aa - at offset 03 */ | |
1389 | ||
1390 | build_append_byte(var->buf, Hex2Byte(uuid + 11)); /* ff - at offset 04 */ | |
1391 | build_append_byte(var->buf, Hex2Byte(uuid + 9)); /* ee - at offset 05 */ | |
1392 | ||
1393 | build_append_byte(var->buf, Hex2Byte(uuid + 16)); /* hh - at offset 06 */ | |
1394 | build_append_byte(var->buf, Hex2Byte(uuid + 14)); /* gg - at offset 07 */ | |
1395 | ||
1396 | build_append_byte(var->buf, Hex2Byte(uuid + 19)); /* ii - at offset 08 */ | |
1397 | build_append_byte(var->buf, Hex2Byte(uuid + 21)); /* jj - at offset 09 */ | |
1398 | ||
1399 | build_append_byte(var->buf, Hex2Byte(uuid + 24)); /* kk - at offset 10 */ | |
1400 | build_append_byte(var->buf, Hex2Byte(uuid + 26)); /* ll - at offset 11 */ | |
1401 | build_append_byte(var->buf, Hex2Byte(uuid + 28)); /* mm - at offset 12 */ | |
1402 | build_append_byte(var->buf, Hex2Byte(uuid + 30)); /* nn - at offset 13 */ | |
1403 | build_append_byte(var->buf, Hex2Byte(uuid + 32)); /* oo - at offset 14 */ | |
1404 | build_append_byte(var->buf, Hex2Byte(uuid + 34)); /* pp - at offset 15 */ | |
1405 | ||
1406 | return var; | |
1407 | } | |
1408 | ||
e1f776c4 SZ |
1409 | /* |
1410 | * ACPI 2.0b: 16.2.3.6.4.3 Unicode Macro (Convert Ascii String To Unicode) | |
1411 | */ | |
1412 | Aml *aml_unicode(const char *str) | |
1413 | { | |
1414 | int i = 0; | |
1415 | Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER); | |
1416 | ||
1417 | do { | |
1418 | build_append_byte(var->buf, str[i]); | |
1419 | build_append_byte(var->buf, 0); | |
1420 | i++; | |
1421 | } while (i <= strlen(str)); | |
1422 | ||
1423 | return var; | |
1424 | } | |
1425 | ||
7bc6fd24 IM |
1426 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefRefOf */ |
1427 | Aml *aml_refof(Aml *arg) | |
1428 | { | |
1429 | Aml *var = aml_opcode(0x71 /* RefOfOp */); | |
1430 | aml_append(var, arg); | |
1431 | return var; | |
1432 | } | |
1433 | ||
95cb0661 XG |
1434 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDerefOf */ |
1435 | Aml *aml_derefof(Aml *arg) | |
1436 | { | |
1437 | Aml *var = aml_opcode(0x83 /* DerefOfOp */); | |
1438 | aml_append(var, arg); | |
1439 | return var; | |
1440 | } | |
1441 | ||
52483d14 XG |
1442 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSizeOf */ |
1443 | Aml *aml_sizeof(Aml *arg) | |
1444 | { | |
1445 | Aml *var = aml_opcode(0x87 /* SizeOfOp */); | |
1446 | aml_append(var, arg); | |
1447 | return var; | |
1448 | } | |
1449 | ||
6e1db3f2 XG |
1450 | /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMutex */ |
1451 | Aml *aml_mutex(const char *name, uint8_t sync_level) | |
1452 | { | |
1453 | Aml *var = aml_alloc(); | |
1454 | build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */ | |
1455 | build_append_byte(var->buf, 0x01); /* MutexOp */ | |
1456 | build_append_namestring(var->buf, "%s", name); | |
1457 | assert(!(sync_level & 0xF0)); | |
1458 | build_append_byte(var->buf, sync_level); | |
1459 | return var; | |
1460 | } | |
1461 | ||
1462 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAcquire */ | |
1463 | Aml *aml_acquire(Aml *mutex, uint16_t timeout) | |
1464 | { | |
1465 | Aml *var = aml_alloc(); | |
1466 | build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */ | |
1467 | build_append_byte(var->buf, 0x23); /* AcquireOp */ | |
1468 | aml_append(var, mutex); | |
1469 | build_append_int_noprefix(var->buf, timeout, sizeof(timeout)); | |
1470 | return var; | |
1471 | } | |
1472 | ||
1473 | /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefRelease */ | |
1474 | Aml *aml_release(Aml *mutex) | |
1475 | { | |
1476 | Aml *var = aml_alloc(); | |
1477 | build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */ | |
1478 | build_append_byte(var->buf, 0x27); /* ReleaseOp */ | |
1479 | aml_append(var, mutex); | |
1480 | return var; | |
1481 | } | |
1482 | ||
67a5c0fa IM |
1483 | /* ACPI 1.0b: 16.2.5.1 Name Space Modifier Objects Encoding: DefAlias */ |
1484 | Aml *aml_alias(const char *source_object, const char *alias_object) | |
1485 | { | |
1486 | Aml *var = aml_opcode(0x06 /* AliasOp */); | |
1487 | aml_append(var, aml_name("%s", source_object)); | |
1488 | aml_append(var, aml_name("%s", alias_object)); | |
1489 | return var; | |
9815cba5 XG |
1490 | } |
1491 | ||
1492 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefConcat */ | |
1493 | Aml *aml_concatenate(Aml *source1, Aml *source2, Aml *target) | |
1494 | { | |
1495 | return build_opcode_2arg_dst(0x73 /* ConcatOp */, source1, source2, | |
1496 | target); | |
67a5c0fa IM |
1497 | } |
1498 | ||
b265f27c XG |
1499 | /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefObjectType */ |
1500 | Aml *aml_object_type(Aml *object) | |
1501 | { | |
1502 | Aml *var = aml_opcode(0x8E /* ObjectTypeOp */); | |
1503 | aml_append(var, object); | |
1504 | return var; | |
1505 | } | |
1506 | ||
658c2718 | 1507 | void |
0e9b9eda | 1508 | build_header(BIOSLinker *linker, GArray *table_data, |
8870ca0e | 1509 | AcpiTableHeader *h, const char *sig, int len, uint8_t rev, |
37ad223c | 1510 | const char *oem_id, const char *oem_table_id) |
658c2718 | 1511 | { |
28213cb6 IM |
1512 | unsigned tbl_offset = (char *)h - table_data->data; |
1513 | unsigned checksum_offset = (char *)&h->checksum - table_data->data; | |
658c2718 SZ |
1514 | memcpy(&h->signature, sig, 4); |
1515 | h->length = cpu_to_le32(len); | |
1516 | h->revision = rev; | |
37ad223c LE |
1517 | |
1518 | if (oem_id) { | |
1519 | strncpy((char *)h->oem_id, oem_id, sizeof h->oem_id); | |
1520 | } else { | |
1521 | memcpy(h->oem_id, ACPI_BUILD_APPNAME6, 6); | |
1522 | } | |
8870ca0e XG |
1523 | |
1524 | if (oem_table_id) { | |
1525 | strncpy((char *)h->oem_table_id, oem_table_id, sizeof(h->oem_table_id)); | |
1526 | } else { | |
1527 | memcpy(h->oem_table_id, ACPI_BUILD_APPNAME4, 4); | |
1528 | memcpy(h->oem_table_id + 4, sig, 4); | |
1529 | } | |
1530 | ||
658c2718 SZ |
1531 | h->oem_revision = cpu_to_le32(1); |
1532 | memcpy(h->asl_compiler_id, ACPI_BUILD_APPNAME4, 4); | |
1533 | h->asl_compiler_revision = cpu_to_le32(1); | |
658c2718 SZ |
1534 | /* Checksum to be filled in by Guest linker */ |
1535 | bios_linker_loader_add_checksum(linker, ACPI_BUILD_TABLE_FILE, | |
28213cb6 | 1536 | tbl_offset, len, checksum_offset); |
658c2718 SZ |
1537 | } |
1538 | ||
1539 | void *acpi_data_push(GArray *table_data, unsigned size) | |
1540 | { | |
1541 | unsigned off = table_data->len; | |
1542 | g_array_set_size(table_data, off + size); | |
1543 | return table_data->data + off; | |
1544 | } | |
1545 | ||
1546 | unsigned acpi_data_len(GArray *table) | |
1547 | { | |
658c2718 | 1548 | assert(g_array_get_element_size(table) == 1); |
658c2718 SZ |
1549 | return table->len; |
1550 | } | |
1551 | ||
1552 | void acpi_add_table(GArray *table_offsets, GArray *table_data) | |
1553 | { | |
4678124b | 1554 | uint32_t offset = table_data->len; |
658c2718 SZ |
1555 | g_array_append_val(table_offsets, offset); |
1556 | } | |
1557 | ||
1558 | void acpi_build_tables_init(AcpiBuildTables *tables) | |
1559 | { | |
1560 | tables->rsdp = g_array_new(false, true /* clear */, 1); | |
1561 | tables->table_data = g_array_new(false, true /* clear */, 1); | |
1562 | tables->tcpalog = g_array_new(false, true /* clear */, 1); | |
c7809e6c | 1563 | tables->vmgenid = g_array_new(false, true /* clear */, 1); |
658c2718 SZ |
1564 | tables->linker = bios_linker_loader_init(); |
1565 | } | |
1566 | ||
1567 | void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre) | |
1568 | { | |
8cc87c31 | 1569 | bios_linker_loader_cleanup(tables->linker); |
658c2718 SZ |
1570 | g_array_free(tables->rsdp, true); |
1571 | g_array_free(tables->table_data, true); | |
1572 | g_array_free(tables->tcpalog, mfre); | |
c7809e6c | 1573 | g_array_free(tables->vmgenid, mfre); |
658c2718 | 1574 | } |
243bdb79 SZ |
1575 | |
1576 | /* Build rsdt table */ | |
1577 | void | |
0e9b9eda | 1578 | build_rsdt(GArray *table_data, BIOSLinker *linker, GArray *table_offsets, |
51513558 | 1579 | const char *oem_id, const char *oem_table_id) |
243bdb79 | 1580 | { |
243bdb79 | 1581 | int i; |
4678124b IM |
1582 | unsigned rsdt_entries_offset; |
1583 | AcpiRsdtDescriptorRev1 *rsdt; | |
1584 | const unsigned table_data_len = (sizeof(uint32_t) * table_offsets->len); | |
1585 | const unsigned rsdt_entry_size = sizeof(rsdt->table_offset_entry[0]); | |
1586 | const size_t rsdt_len = sizeof(*rsdt) + table_data_len; | |
243bdb79 | 1587 | |
243bdb79 | 1588 | rsdt = acpi_data_push(table_data, rsdt_len); |
4678124b | 1589 | rsdt_entries_offset = (char *)rsdt->table_offset_entry - table_data->data; |
243bdb79 | 1590 | for (i = 0; i < table_offsets->len; ++i) { |
4678124b IM |
1591 | uint32_t ref_tbl_offset = g_array_index(table_offsets, uint32_t, i); |
1592 | uint32_t rsdt_entry_offset = rsdt_entries_offset + rsdt_entry_size * i; | |
1593 | ||
243bdb79 SZ |
1594 | /* rsdt->table_offset_entry to be filled by Guest linker */ |
1595 | bios_linker_loader_add_pointer(linker, | |
4678124b IM |
1596 | ACPI_BUILD_TABLE_FILE, rsdt_entry_offset, rsdt_entry_size, |
1597 | ACPI_BUILD_TABLE_FILE, ref_tbl_offset); | |
243bdb79 SZ |
1598 | } |
1599 | build_header(linker, table_data, | |
51513558 | 1600 | (void *)rsdt, "RSDT", rsdt_len, 1, oem_id, oem_table_id); |
243bdb79 | 1601 | } |
64b83136 SZ |
1602 | |
1603 | void build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base, | |
1604 | uint64_t len, int node, MemoryAffinityFlags flags) | |
1605 | { | |
1606 | numamem->type = ACPI_SRAT_MEMORY; | |
1607 | numamem->length = sizeof(*numamem); | |
1608 | numamem->proximity = cpu_to_le32(node); | |
1609 | numamem->flags = cpu_to_le32(flags); | |
1610 | numamem->base_addr = cpu_to_le64(base); | |
1611 | numamem->range_length = cpu_to_le64(len); | |
1612 | } | |
0f203430 HC |
1613 | |
1614 | /* | |
1615 | * ACPI spec 5.2.17 System Locality Distance Information Table | |
1616 | * (Revision 2.0 or later) | |
1617 | */ | |
1618 | void build_slit(GArray *table_data, BIOSLinker *linker) | |
1619 | { | |
1620 | int slit_start, i, j; | |
1621 | slit_start = table_data->len; | |
1622 | ||
1623 | acpi_data_push(table_data, sizeof(AcpiTableHeader)); | |
1624 | ||
1625 | build_append_int_noprefix(table_data, nb_numa_nodes, 8); | |
1626 | for (i = 0; i < nb_numa_nodes; i++) { | |
1627 | for (j = 0; j < nb_numa_nodes; j++) { | |
1628 | assert(numa_info[i].distance[j]); | |
1629 | build_append_int_noprefix(table_data, numa_info[i].distance[j], 1); | |
1630 | } | |
1631 | } | |
1632 | ||
1633 | build_header(linker, table_data, | |
1634 | (void *)(table_data->data + slit_start), | |
1635 | "SLIT", | |
1636 | table_data->len - slit_start, 1, NULL, NULL); | |
1637 | } |