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 10 #define ACPI_BUILD_APPNAME6 "BOCHS " 11 #define ACPI_BUILD_APPNAME4 "BXPC" 12 13 #define ACPI_BUILD_TABLE_FILE "etc/acpi/tables" 14 #define ACPI_BUILD_RSDP_FILE "etc/acpi/rsdp" 15 #define ACPI_BUILD_TPMLOG_FILE "etc/tpm/log" 16 #define ACPI_BUILD_LOADER_FILE "etc/table-loader" 17 18 #define AML_NOTIFY_METHOD "NTFY" 19 20 typedef enum { 21 AML_NO_OPCODE = 0,/* has only data */ 22 AML_OPCODE, /* has opcode optionally followed by data */ 23 AML_PACKAGE, /* has opcode and uses PkgLength for its length */ 24 AML_EXT_PACKAGE, /* Same as AML_PACKAGE but also has 'ExOpPrefix' */ 25 AML_BUFFER, /* data encoded as 'DefBuffer' */ 26 AML_RES_TEMPLATE, /* encoded as ResourceTemplate macro */ 27 } AmlBlockFlags; 28 29 struct Aml { 30 GArray *buf; 31 32 /*< private >*/ 33 uint8_t op; 34 AmlBlockFlags block_flags; 35 }; 36 typedef struct Aml Aml; 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 BIOSLinker *linker; 225 } AcpiBuildTables; 226 227 /* 228 * ACPI 5.0: 6.4.3.8.2 Serial Bus Connection Descriptors 229 * Serial Bus Type 230 */ 231 #define AML_SERIAL_BUS_TYPE_I2C 1 232 #define AML_SERIAL_BUS_TYPE_SPI 2 233 #define AML_SERIAL_BUS_TYPE_UART 3 234 235 /* 236 * ACPI 5.0: 6.4.3.8.2 Serial Bus Connection Descriptors 237 * General Flags 238 */ 239 /* Slave Mode */ 240 #define AML_SERIAL_BUS_FLAG_MASTER_DEVICE (1 << 0) 241 /* Consumer/Producer */ 242 #define AML_SERIAL_BUS_FLAG_CONSUME_ONLY (1 << 1) 243 244 /** 245 * init_aml_allocator: 246 * 247 * Called for initializing API allocator which allow to use 248 * AML API. 249 * Returns: toplevel container which accumulates all other 250 * AML elements for a table. 251 */ 252 Aml *init_aml_allocator(void); 253 254 /** 255 * free_aml_allocator: 256 * 257 * Releases all elements used by AML API, frees associated memory 258 * and invalidates AML allocator. After this call @init_aml_allocator 259 * should be called again if AML API is to be used again. 260 */ 261 void free_aml_allocator(void); 262 263 /** 264 * aml_append: 265 * @parent_ctx: context to which @child element is added 266 * @child: element that is copied into @parent_ctx context 267 * 268 * Joins Aml elements together and helps to construct AML tables 269 * Examle of usage: 270 * Aml *table = aml_def_block("SSDT", ...); 271 * Aml *sb = aml_scope("\\_SB"); 272 * Aml *dev = aml_device("PCI0"); 273 * 274 * aml_append(dev, aml_name_decl("HID", aml_eisaid("PNP0A03"))); 275 * aml_append(sb, dev); 276 * aml_append(table, sb); 277 */ 278 void aml_append(Aml *parent_ctx, Aml *child); 279 280 /* non block AML object primitives */ 281 Aml *aml_name(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 282 Aml *aml_name_decl(const char *name, Aml *val); 283 Aml *aml_debug(void); 284 Aml *aml_return(Aml *val); 285 Aml *aml_int(const uint64_t val); 286 Aml *aml_arg(int pos); 287 Aml *aml_to_integer(Aml *arg); 288 Aml *aml_to_hexstring(Aml *src, Aml *dst); 289 Aml *aml_to_buffer(Aml *src, Aml *dst); 290 Aml *aml_store(Aml *val, Aml *target); 291 Aml *aml_and(Aml *arg1, Aml *arg2, Aml *dst); 292 Aml *aml_or(Aml *arg1, Aml *arg2, Aml *dst); 293 Aml *aml_lor(Aml *arg1, Aml *arg2); 294 Aml *aml_shiftleft(Aml *arg1, Aml *count); 295 Aml *aml_shiftright(Aml *arg1, Aml *count, Aml *dst); 296 Aml *aml_lless(Aml *arg1, Aml *arg2); 297 Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst); 298 Aml *aml_subtract(Aml *arg1, Aml *arg2, Aml *dst); 299 Aml *aml_increment(Aml *arg); 300 Aml *aml_decrement(Aml *arg); 301 Aml *aml_index(Aml *arg1, Aml *idx); 302 Aml *aml_notify(Aml *arg1, Aml *arg2); 303 Aml *aml_call0(const char *method); 304 Aml *aml_call1(const char *method, Aml *arg1); 305 Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2); 306 Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3); 307 Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4); 308 Aml *aml_call5(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4, 309 Aml *arg5); 310 Aml *aml_gpio_int(AmlConsumerAndProducer con_and_pro, 311 AmlLevelAndEdge edge_level, 312 AmlActiveHighAndLow active_level, AmlShared shared, 313 AmlPinConfig pin_config, uint16_t debounce_timeout, 314 const uint32_t pin_list[], uint32_t pin_count, 315 const char *resource_source_name, 316 const uint8_t *vendor_data, uint16_t vendor_data_len); 317 Aml *aml_memory32_fixed(uint32_t addr, uint32_t size, 318 AmlReadAndWrite read_and_write); 319 Aml *aml_interrupt(AmlConsumerAndProducer con_and_pro, 320 AmlLevelAndEdge level_and_edge, 321 AmlActiveHighAndLow high_and_low, AmlShared shared, 322 uint32_t *irq_list, uint8_t irq_count); 323 Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, 324 uint8_t aln, uint8_t len); 325 Aml *aml_operation_region(const char *name, AmlRegionSpace rs, 326 Aml *offset, uint32_t len); 327 Aml *aml_irq_no_flags(uint8_t irq); 328 Aml *aml_named_field(const char *name, unsigned length); 329 Aml *aml_reserved_field(unsigned length); 330 Aml *aml_local(int num); 331 Aml *aml_string(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 332 Aml *aml_lnot(Aml *arg); 333 Aml *aml_equal(Aml *arg1, Aml *arg2); 334 Aml *aml_lgreater(Aml *arg1, Aml *arg2); 335 Aml *aml_lgreater_equal(Aml *arg1, Aml *arg2); 336 Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len, 337 const char *name_format, ...) GCC_FMT_ATTR(4, 5); 338 Aml *aml_eisaid(const char *str); 339 Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, 340 AmlDecode dec, uint16_t addr_gran, 341 uint16_t addr_min, uint16_t addr_max, 342 uint16_t addr_trans, uint16_t len); 343 Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, 344 AmlDecode dec, AmlISARanges isa_ranges, 345 uint16_t addr_gran, uint16_t addr_min, 346 uint16_t addr_max, uint16_t addr_trans, 347 uint16_t len); 348 Aml *aml_dword_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, 349 AmlDecode dec, AmlISARanges isa_ranges, 350 uint32_t addr_gran, uint32_t addr_min, 351 uint32_t addr_max, uint32_t addr_trans, 352 uint32_t len); 353 Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed, 354 AmlMaxFixed max_fixed, AmlCacheable cacheable, 355 AmlReadAndWrite read_and_write, 356 uint32_t addr_gran, uint32_t addr_min, 357 uint32_t addr_max, uint32_t addr_trans, 358 uint32_t len); 359 Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed, 360 AmlMaxFixed max_fixed, AmlCacheable cacheable, 361 AmlReadAndWrite read_and_write, 362 uint64_t addr_gran, uint64_t addr_min, 363 uint64_t addr_max, uint64_t addr_trans, 364 uint64_t len); 365 Aml *aml_dma(AmlDmaType typ, AmlDmaBusMaster bm, AmlTransferSize sz, 366 uint8_t channel); 367 Aml *aml_sleep(uint64_t msec); 368 Aml *aml_i2c_serial_bus_device(uint16_t address, const char *resource_source); 369 370 /* Block AML object primitives */ 371 Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 372 Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 373 Aml *aml_method(const char *name, int arg_count, AmlSerializeFlag sflag); 374 Aml *aml_if(Aml *predicate); 375 Aml *aml_else(void); 376 Aml *aml_while(Aml *predicate); 377 Aml *aml_package(uint8_t num_elements); 378 Aml *aml_buffer(int buffer_size, uint8_t *byte_list); 379 Aml *aml_resource_template(void); 380 Aml *aml_field(const char *name, AmlAccessType type, AmlLockRule lock, 381 AmlUpdateRule rule); 382 Aml *aml_mutex(const char *name, uint8_t sync_level); 383 Aml *aml_acquire(Aml *mutex, uint16_t timeout); 384 Aml *aml_release(Aml *mutex); 385 Aml *aml_alias(const char *source_object, const char *alias_object); 386 Aml *aml_create_field(Aml *srcbuf, Aml *bit_index, Aml *num_bits, 387 const char *name); 388 Aml *aml_create_dword_field(Aml *srcbuf, Aml *index, const char *name); 389 Aml *aml_create_qword_field(Aml *srcbuf, Aml *index, const char *name); 390 Aml *aml_varpackage(uint32_t num_elements); 391 Aml *aml_touuid(const char *uuid); 392 Aml *aml_unicode(const char *str); 393 Aml *aml_refof(Aml *arg); 394 Aml *aml_derefof(Aml *arg); 395 Aml *aml_sizeof(Aml *arg); 396 Aml *aml_concatenate(Aml *source1, Aml *source2, Aml *target); 397 Aml *aml_object_type(Aml *object); 398 399 void build_append_int_noprefix(GArray *table, uint64_t value, int size); 400 void 401 build_header(BIOSLinker *linker, GArray *table_data, 402 AcpiTableHeader *h, const char *sig, int len, uint8_t rev, 403 const char *oem_id, const char *oem_table_id); 404 void *acpi_data_push(GArray *table_data, unsigned size); 405 unsigned acpi_data_len(GArray *table); 406 void acpi_add_table(GArray *table_offsets, GArray *table_data); 407 void acpi_build_tables_init(AcpiBuildTables *tables); 408 void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre); 409 void 410 build_rsdp(GArray *tbl, BIOSLinker *linker, AcpiRsdpData *rsdp_data); 411 void 412 build_rsdt(GArray *table_data, BIOSLinker *linker, GArray *table_offsets, 413 const char *oem_id, const char *oem_table_id); 414 void 415 build_xsdt(GArray *table_data, BIOSLinker *linker, GArray *table_offsets, 416 const char *oem_id, const char *oem_table_id); 417 418 int 419 build_append_named_dword(GArray *array, const char *name_format, ...) 420 GCC_FMT_ATTR(2, 3); 421 422 void build_append_gas(GArray *table, AmlAddressSpace as, 423 uint8_t bit_width, uint8_t bit_offset, 424 uint8_t access_width, uint64_t address); 425 426 static inline void 427 build_append_gas_from_struct(GArray *table, const struct AcpiGenericAddress *s) 428 { 429 build_append_gas(table, s->space_id, s->bit_width, s->bit_offset, 430 s->access_width, s->address); 431 } 432 433 void build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base, 434 uint64_t len, int node, MemoryAffinityFlags flags); 435 436 void build_slit(GArray *table_data, BIOSLinker *linker, MachineState *ms); 437 438 void build_fadt(GArray *tbl, BIOSLinker *linker, const AcpiFadtData *f, 439 const char *oem_id, const char *oem_table_id); 440 #endif 441