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