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