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