1 #ifndef HW_ACPI_AML_BUILD_H 2 #define HW_ACPI_AML_BUILD_H 3 4 #include "hw/acpi/acpi-defs.h" 5 #include "hw/acpi/bios-linker-loader.h" 6 7 #define ACPI_BUILD_APPNAME6 "BOCHS " 8 #define ACPI_BUILD_APPNAME8 "BXPC " 9 10 #define ACPI_BUILD_TABLE_FILE "etc/acpi/tables" 11 #define ACPI_BUILD_RSDP_FILE "etc/acpi/rsdp" 12 #define ACPI_BUILD_TPMLOG_FILE "etc/tpm/log" 13 #define ACPI_BUILD_LOADER_FILE "etc/table-loader" 14 15 #define AML_NOTIFY_METHOD "NTFY" 16 17 typedef enum { 18 AML_NO_OPCODE = 0,/* has only data */ 19 AML_OPCODE, /* has opcode optionally followed by data */ 20 AML_PACKAGE, /* has opcode and uses PkgLength for its length */ 21 AML_EXT_PACKAGE, /* Same as AML_PACKAGE but also has 'ExOpPrefix' */ 22 AML_BUFFER, /* data encoded as 'DefBuffer' */ 23 AML_RES_TEMPLATE, /* encoded as ResourceTemplate macro */ 24 } AmlBlockFlags; 25 26 struct Aml { 27 GArray *buf; 28 29 /*< private >*/ 30 uint8_t op; 31 AmlBlockFlags block_flags; 32 }; 33 34 typedef enum { 35 AML_COMPATIBILITY = 0, 36 AML_TYPEA = 1, 37 AML_TYPEB = 2, 38 AML_TYPEF = 3, 39 } AmlDmaType; 40 41 typedef enum { 42 AML_NOTBUSMASTER = 0, 43 AML_BUSMASTER = 1, 44 } AmlDmaBusMaster; 45 46 typedef enum { 47 AML_TRANSFER8 = 0, 48 AML_TRANSFER8_16 = 1, 49 AML_TRANSFER16 = 2, 50 } AmlTransferSize; 51 52 typedef enum { 53 AML_DECODE10 = 0, 54 AML_DECODE16 = 1, 55 } AmlIODecode; 56 57 typedef enum { 58 AML_ANY_ACC = 0, 59 AML_BYTE_ACC = 1, 60 AML_WORD_ACC = 2, 61 AML_DWORD_ACC = 3, 62 AML_QWORD_ACC = 4, 63 AML_BUFFER_ACC = 5, 64 } AmlAccessType; 65 66 typedef enum { 67 AML_NOLOCK = 0, 68 AML_LOCK = 1, 69 } AmlLockRule; 70 71 typedef enum { 72 AML_PRESERVE = 0, 73 AML_WRITE_AS_ONES = 1, 74 AML_WRITE_AS_ZEROS = 2, 75 } AmlUpdateRule; 76 77 typedef enum { 78 AML_AS_SYSTEM_MEMORY = 0X00, 79 AML_AS_SYSTEM_IO = 0X01, 80 AML_AS_PCI_CONFIG = 0X02, 81 AML_AS_EMBEDDED_CTRL = 0X03, 82 AML_AS_SMBUS = 0X04, 83 AML_AS_FFH = 0X7F, 84 } AmlAddressSpace; 85 86 typedef enum { 87 AML_SYSTEM_MEMORY = 0X00, 88 AML_SYSTEM_IO = 0X01, 89 AML_PCI_CONFIG = 0X02, 90 } AmlRegionSpace; 91 92 typedef enum { 93 AML_MEMORY_RANGE = 0, 94 AML_IO_RANGE = 1, 95 AML_BUS_NUMBER_RANGE = 2, 96 } AmlResourceType; 97 98 typedef enum { 99 AML_SUB_DECODE = 1 << 1, 100 AML_POS_DECODE = 0 101 } AmlDecode; 102 103 typedef enum { 104 AML_MAX_FIXED = 1 << 3, 105 AML_MAX_NOT_FIXED = 0, 106 } AmlMaxFixed; 107 108 typedef enum { 109 AML_MIN_FIXED = 1 << 2, 110 AML_MIN_NOT_FIXED = 0 111 } AmlMinFixed; 112 113 /* 114 * ACPI 1.0b: Table 6-26 I/O Resource Flag (Resource Type = 1) Definitions 115 * _RNG field definition 116 */ 117 typedef enum { 118 AML_ISA_ONLY = 1, 119 AML_NON_ISA_ONLY = 2, 120 AML_ENTIRE_RANGE = 3, 121 } AmlISARanges; 122 123 /* 124 * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions 125 * _MEM field definition 126 */ 127 typedef enum { 128 AML_NON_CACHEABLE = 0, 129 AML_CACHEABLE = 1, 130 AML_WRITE_COMBINING = 2, 131 AML_PREFETCHABLE = 3, 132 } AmlCacheable; 133 134 /* 135 * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions 136 * _RW field definition 137 */ 138 typedef enum { 139 AML_READ_ONLY = 0, 140 AML_READ_WRITE = 1, 141 } AmlReadAndWrite; 142 143 /* 144 * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition 145 * Interrupt Vector Flags Bits[0] Consumer/Producer 146 */ 147 typedef enum { 148 AML_CONSUMER_PRODUCER = 0, 149 AML_CONSUMER = 1, 150 } AmlConsumerAndProducer; 151 152 /* 153 * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition 154 * _HE field definition 155 */ 156 typedef enum { 157 AML_LEVEL = 0, 158 AML_EDGE = 1, 159 } AmlLevelAndEdge; 160 161 /* 162 * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition 163 * _LL field definition 164 */ 165 typedef enum { 166 AML_ACTIVE_HIGH = 0, 167 AML_ACTIVE_LOW = 1, 168 } AmlActiveHighAndLow; 169 170 /* 171 * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition 172 * _SHR field definition 173 */ 174 typedef enum { 175 AML_EXCLUSIVE = 0, 176 AML_SHARED = 1, 177 AML_EXCLUSIVE_AND_WAKE = 2, 178 AML_SHARED_AND_WAKE = 3, 179 } AmlShared; 180 181 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: MethodFlags */ 182 typedef enum { 183 AML_NOTSERIALIZED = 0, 184 AML_SERIALIZED = 1, 185 } AmlSerializeFlag; 186 187 /* 188 * ACPI 5.0: Table 6-189 GPIO Connection Descriptor Definition 189 * GPIO Connection Type 190 */ 191 typedef enum { 192 AML_INTERRUPT_CONNECTION = 0, 193 AML_IO_CONNECTION = 1, 194 } AmlGpioConnectionType; 195 196 /* 197 * ACPI 5.0: Table 6-189 GPIO Connection Descriptor Definition 198 * _PPI field definition 199 */ 200 typedef enum { 201 AML_PULL_DEFAULT = 0, 202 AML_PULL_UP = 1, 203 AML_PULL_DOWN = 2, 204 AML_PULL_NONE = 3, 205 } AmlPinConfig; 206 207 typedef enum { 208 MEM_AFFINITY_NOFLAGS = 0, 209 MEM_AFFINITY_ENABLED = (1 << 0), 210 MEM_AFFINITY_HOTPLUGGABLE = (1 << 1), 211 MEM_AFFINITY_NON_VOLATILE = (1 << 2), 212 } MemoryAffinityFlags; 213 214 typedef 215 struct AcpiBuildTables { 216 GArray *table_data; 217 GArray *rsdp; 218 GArray *tcpalog; 219 GArray *vmgenid; 220 GArray *hardware_errors; 221 BIOSLinker *linker; 222 } AcpiBuildTables; 223 224 typedef 225 struct CrsRangeEntry { 226 uint64_t base; 227 uint64_t limit; 228 } CrsRangeEntry; 229 230 typedef 231 struct CrsRangeSet { 232 GPtrArray *io_ranges; 233 GPtrArray *mem_ranges; 234 GPtrArray *mem_64bit_ranges; 235 } CrsRangeSet; 236 237 238 /* 239 * ACPI 5.0: 6.4.3.8.2 Serial Bus Connection Descriptors 240 * Serial Bus Type 241 */ 242 #define AML_SERIAL_BUS_TYPE_I2C 1 243 #define AML_SERIAL_BUS_TYPE_SPI 2 244 #define AML_SERIAL_BUS_TYPE_UART 3 245 246 /* 247 * ACPI 5.0: 6.4.3.8.2 Serial Bus Connection Descriptors 248 * General Flags 249 */ 250 /* Slave Mode */ 251 #define AML_SERIAL_BUS_FLAG_MASTER_DEVICE (1 << 0) 252 /* Consumer/Producer */ 253 #define AML_SERIAL_BUS_FLAG_CONSUME_ONLY (1 << 1) 254 255 #define ACPI_APEI_ERROR_DEVICE "GEDD" 256 /** 257 * init_aml_allocator: 258 * 259 * Called for initializing API allocator which allow to use 260 * AML API. 261 * Returns: toplevel container which accumulates all other 262 * AML elements for a table. 263 */ 264 Aml *init_aml_allocator(void); 265 266 /** 267 * free_aml_allocator: 268 * 269 * Releases all elements used by AML API, frees associated memory 270 * and invalidates AML allocator. After this call @init_aml_allocator 271 * should be called again if AML API is to be used again. 272 */ 273 void free_aml_allocator(void); 274 275 /** 276 * aml_append: 277 * @parent_ctx: context to which @child element is added 278 * @child: element that is copied into @parent_ctx context 279 * 280 * Joins Aml elements together and helps to construct AML tables 281 * Example of usage: 282 * Aml *table = aml_def_block("SSDT", ...); 283 * Aml *sb = aml_scope("\\_SB"); 284 * Aml *dev = aml_device("PCI0"); 285 * 286 * aml_append(dev, aml_name_decl("HID", aml_eisaid("PNP0A03"))); 287 * aml_append(sb, dev); 288 * aml_append(table, sb); 289 */ 290 void aml_append(Aml *parent_ctx, Aml *child); 291 292 /* non block AML object primitives */ 293 Aml *aml_name(const char *name_format, ...) G_GNUC_PRINTF(1, 2); 294 Aml *aml_name_decl(const char *name, Aml *val); 295 Aml *aml_debug(void); 296 Aml *aml_return(Aml *val); 297 Aml *aml_int(const uint64_t val); 298 Aml *aml_arg(int pos); 299 Aml *aml_to_integer(Aml *arg); 300 Aml *aml_to_hexstring(Aml *src, Aml *dst); 301 Aml *aml_to_buffer(Aml *src, Aml *dst); 302 Aml *aml_to_decimalstring(Aml *src, Aml *dst); 303 Aml *aml_store(Aml *val, Aml *target); 304 Aml *aml_and(Aml *arg1, Aml *arg2, Aml *dst); 305 Aml *aml_or(Aml *arg1, Aml *arg2, Aml *dst); 306 Aml *aml_land(Aml *arg1, Aml *arg2); 307 Aml *aml_lor(Aml *arg1, Aml *arg2); 308 Aml *aml_shiftleft(Aml *arg1, Aml *count); 309 Aml *aml_shiftright(Aml *arg1, Aml *count, Aml *dst); 310 Aml *aml_lless(Aml *arg1, Aml *arg2); 311 Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst); 312 Aml *aml_subtract(Aml *arg1, Aml *arg2, Aml *dst); 313 Aml *aml_increment(Aml *arg); 314 Aml *aml_decrement(Aml *arg); 315 Aml *aml_index(Aml *arg1, Aml *idx); 316 Aml *aml_notify(Aml *arg1, Aml *arg2); 317 Aml *aml_break(void); 318 Aml *aml_call0(const char *method); 319 Aml *aml_call1(const char *method, Aml *arg1); 320 Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2); 321 Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3); 322 Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4); 323 Aml *aml_call5(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4, 324 Aml *arg5); 325 Aml *aml_call6(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4, 326 Aml *arg5, Aml *arg6); 327 Aml *aml_gpio_int(AmlConsumerAndProducer con_and_pro, 328 AmlLevelAndEdge edge_level, 329 AmlActiveHighAndLow active_level, AmlShared shared, 330 AmlPinConfig pin_config, uint16_t debounce_timeout, 331 const uint32_t pin_list[], uint32_t pin_count, 332 const char *resource_source_name, 333 const uint8_t *vendor_data, uint16_t vendor_data_len); 334 Aml *aml_memory32_fixed(uint32_t addr, uint32_t size, 335 AmlReadAndWrite read_and_write); 336 Aml *aml_interrupt(AmlConsumerAndProducer con_and_pro, 337 AmlLevelAndEdge level_and_edge, 338 AmlActiveHighAndLow high_and_low, AmlShared shared, 339 uint32_t *irq_list, uint8_t irq_count); 340 Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, 341 uint8_t aln, uint8_t len); 342 Aml *aml_operation_region(const char *name, AmlRegionSpace rs, 343 Aml *offset, uint32_t len); 344 Aml *aml_irq_no_flags(uint8_t irq); 345 Aml *aml_named_field(const char *name, unsigned length); 346 Aml *aml_reserved_field(unsigned length); 347 Aml *aml_local(int num); 348 Aml *aml_string(const char *name_format, ...) G_GNUC_PRINTF(1, 2); 349 Aml *aml_lnot(Aml *arg); 350 Aml *aml_equal(Aml *arg1, Aml *arg2); 351 Aml *aml_lgreater(Aml *arg1, Aml *arg2); 352 Aml *aml_lgreater_equal(Aml *arg1, Aml *arg2); 353 Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len, 354 const char *name_format, ...) G_GNUC_PRINTF(4, 5); 355 Aml *aml_eisaid(const char *str); 356 Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, 357 AmlDecode dec, uint16_t addr_gran, 358 uint16_t addr_min, uint16_t addr_max, 359 uint16_t addr_trans, uint16_t len); 360 Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, 361 AmlDecode dec, AmlISARanges isa_ranges, 362 uint16_t addr_gran, uint16_t addr_min, 363 uint16_t addr_max, uint16_t addr_trans, 364 uint16_t len); 365 Aml *aml_dword_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, 366 AmlDecode dec, AmlISARanges isa_ranges, 367 uint32_t addr_gran, uint32_t addr_min, 368 uint32_t addr_max, uint32_t addr_trans, 369 uint32_t len); 370 Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed, 371 AmlMaxFixed max_fixed, AmlCacheable cacheable, 372 AmlReadAndWrite read_and_write, 373 uint32_t addr_gran, uint32_t addr_min, 374 uint32_t addr_max, uint32_t addr_trans, 375 uint32_t len); 376 Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed, 377 AmlMaxFixed max_fixed, AmlCacheable cacheable, 378 AmlReadAndWrite read_and_write, 379 uint64_t addr_gran, uint64_t addr_min, 380 uint64_t addr_max, uint64_t addr_trans, 381 uint64_t len); 382 Aml *aml_dma(AmlDmaType typ, AmlDmaBusMaster bm, AmlTransferSize sz, 383 uint8_t channel); 384 Aml *aml_sleep(uint64_t msec); 385 Aml *aml_i2c_serial_bus_device(uint16_t address, const char *resource_source); 386 Aml *aml_error_device(void); 387 388 /* Block AML object primitives */ 389 Aml *aml_scope(const char *name_format, ...) G_GNUC_PRINTF(1, 2); 390 Aml *aml_device(const char *name_format, ...) G_GNUC_PRINTF(1, 2); 391 Aml *aml_method(const char *name, int arg_count, AmlSerializeFlag sflag); 392 Aml *aml_if(Aml *predicate); 393 Aml *aml_else(void); 394 Aml *aml_while(Aml *predicate); 395 Aml *aml_package(uint8_t num_elements); 396 Aml *aml_buffer(int buffer_size, uint8_t *byte_list); 397 Aml *aml_resource_template(void); 398 Aml *aml_field(const char *name, AmlAccessType type, AmlLockRule lock, 399 AmlUpdateRule rule); 400 Aml *aml_mutex(const char *name, uint8_t sync_level); 401 Aml *aml_acquire(Aml *mutex, uint16_t timeout); 402 Aml *aml_release(Aml *mutex); 403 Aml *aml_alias(const char *source_object, const char *alias_object); 404 Aml *aml_create_field(Aml *srcbuf, Aml *bit_index, Aml *num_bits, 405 const char *name); 406 Aml *aml_create_dword_field(Aml *srcbuf, Aml *index, const char *name); 407 Aml *aml_create_qword_field(Aml *srcbuf, Aml *index, const char *name); 408 Aml *aml_varpackage(uint32_t num_elements); 409 Aml *aml_touuid(const char *uuid); 410 Aml *aml_unicode(const char *str); 411 Aml *aml_refof(Aml *arg); 412 Aml *aml_derefof(Aml *arg); 413 Aml *aml_sizeof(Aml *arg); 414 Aml *aml_concatenate(Aml *source1, Aml *source2, Aml *target); 415 Aml *aml_object_type(Aml *object); 416 417 void build_append_int_noprefix(GArray *table, uint64_t value, int size); 418 419 typedef struct AcpiTable { 420 const char *sig; 421 const uint8_t rev; 422 const char *oem_id; 423 const char *oem_table_id; 424 /* private vars tracking table state */ 425 GArray *array; 426 unsigned table_offset; 427 } AcpiTable; 428 429 /** 430 * acpi_table_begin: 431 * initializes table header and keeps track of 432 * table data/offsets 433 * @desc: ACPI table descriptor 434 * @array: blob where the ACPI table will be composed/stored. 435 */ 436 void acpi_table_begin(AcpiTable *desc, GArray *array); 437 438 /** 439 * acpi_table_end: 440 * sets actual table length and tells bios loader 441 * where table is for the later initialization on 442 * guest side. 443 * @linker: reference to BIOSLinker object to use for the table 444 * @table: ACPI table descriptor that was used with @acpi_table_begin 445 * counterpart 446 */ 447 void acpi_table_end(BIOSLinker *linker, AcpiTable *table); 448 449 void *acpi_data_push(GArray *table_data, unsigned size); 450 unsigned acpi_data_len(GArray *table); 451 void acpi_add_table(GArray *table_offsets, GArray *table_data); 452 void acpi_build_tables_init(AcpiBuildTables *tables); 453 void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre); 454 void 455 build_rsdp(GArray *tbl, BIOSLinker *linker, AcpiRsdpData *rsdp_data); 456 void 457 build_rsdt(GArray *table_data, BIOSLinker *linker, GArray *table_offsets, 458 const char *oem_id, const char *oem_table_id); 459 void 460 build_xsdt(GArray *table_data, BIOSLinker *linker, GArray *table_offsets, 461 const char *oem_id, const char *oem_table_id); 462 463 int 464 build_append_named_dword(GArray *array, const char *name_format, ...) 465 G_GNUC_PRINTF(2, 3); 466 467 void build_append_gas(GArray *table, AmlAddressSpace as, 468 uint8_t bit_width, uint8_t bit_offset, 469 uint8_t access_width, uint64_t address); 470 471 static inline void 472 build_append_gas_from_struct(GArray *table, const struct AcpiGenericAddress *s) 473 { 474 build_append_gas(table, s->space_id, s->bit_width, s->bit_offset, 475 s->access_width, s->address); 476 } 477 478 void crs_range_insert(GPtrArray *ranges, uint64_t base, uint64_t limit); 479 void crs_replace_with_free_ranges(GPtrArray *ranges, 480 uint64_t start, uint64_t end); 481 void crs_range_set_init(CrsRangeSet *range_set); 482 void crs_range_set_free(CrsRangeSet *range_set); 483 484 Aml *build_crs(PCIHostState *host, CrsRangeSet *range_set, uint32_t io_offset, 485 uint32_t mmio32_offset, uint64_t mmio64_offset, 486 uint16_t bus_nr_offset); 487 488 void build_srat_memory(GArray *table_data, uint64_t base, 489 uint64_t len, int node, MemoryAffinityFlags flags); 490 491 void build_srat_pci_generic_initiator(GArray *table_data, uint32_t node, 492 uint16_t segment, uint8_t bus, 493 uint8_t devfn); 494 495 void build_srat_acpi_generic_port(GArray *table_data, uint32_t node, 496 const char *hid, uint32_t uid); 497 498 void build_slit(GArray *table_data, BIOSLinker *linker, MachineState *ms, 499 const char *oem_id, const char *oem_table_id); 500 501 void build_pptt(GArray *table_data, BIOSLinker *linker, MachineState *ms, 502 const char *oem_id, const char *oem_table_id); 503 504 void build_fadt(GArray *tbl, BIOSLinker *linker, const AcpiFadtData *f, 505 const char *oem_id, const char *oem_table_id); 506 507 void build_tpm2(GArray *table_data, BIOSLinker *linker, GArray *tcpalog, 508 const char *oem_id, const char *oem_table_id); 509 510 void build_spcr(GArray *table_data, BIOSLinker *linker, 511 const AcpiSpcrData *f, const uint8_t rev, 512 const char *oem_id, const char *oem_table_id, const char *name); 513 #endif 514