1 #ifndef HW_ACPI_GEN_UTILS_H 2 #define HW_ACPI_GEN_UTILS_H 3 4 #include <stdint.h> 5 #include <glib.h> 6 #include "qemu/compiler.h" 7 #include "hw/acpi/acpi-defs.h" 8 9 /* Reserve RAM space for tables: add another order of magnitude. */ 10 #define ACPI_BUILD_TABLE_MAX_SIZE 0x200000 11 12 #define ACPI_BUILD_APPNAME6 "BOCHS " 13 #define ACPI_BUILD_APPNAME4 "BXPC" 14 15 #define ACPI_BUILD_TABLE_FILE "etc/acpi/tables" 16 #define ACPI_BUILD_RSDP_FILE "etc/acpi/rsdp" 17 #define ACPI_BUILD_TPMLOG_FILE "etc/tpm/log" 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 typedef struct Aml Aml; 38 39 typedef enum { 40 AML_COMPATIBILITY = 0, 41 AML_TYPEA = 1, 42 AML_TYPEB = 2, 43 AML_TYPEF = 3, 44 } AmlDmaType; 45 46 typedef enum { 47 AML_NOTBUSMASTER = 0, 48 AML_BUSMASTER = 1, 49 } AmlDmaBusMaster; 50 51 typedef enum { 52 AML_TRANSFER8 = 0, 53 AML_TRANSFER8_16 = 1, 54 AML_TRANSFER16 = 2, 55 } AmlTransferSize; 56 57 typedef enum { 58 AML_DECODE10 = 0, 59 AML_DECODE16 = 1, 60 } AmlIODecode; 61 62 typedef enum { 63 AML_ANY_ACC = 0, 64 AML_BYTE_ACC = 1, 65 AML_WORD_ACC = 2, 66 AML_DWORD_ACC = 3, 67 AML_QWORD_ACC = 4, 68 AML_BUFFER_ACC = 5, 69 } AmlAccessType; 70 71 typedef enum { 72 AML_NOLOCK = 0, 73 AML_LOCK = 1, 74 } AmlLockRule; 75 76 typedef enum { 77 AML_PRESERVE = 0, 78 AML_WRITE_AS_ONES = 1, 79 AML_WRITE_AS_ZEROS = 2, 80 } AmlUpdateRule; 81 82 typedef enum { 83 AML_SYSTEM_MEMORY = 0X00, 84 AML_SYSTEM_IO = 0X01, 85 AML_PCI_CONFIG = 0X02, 86 } AmlRegionSpace; 87 88 typedef enum { 89 AML_MEMORY_RANGE = 0, 90 AML_IO_RANGE = 1, 91 AML_BUS_NUMBER_RANGE = 2, 92 } AmlResourceType; 93 94 typedef enum { 95 AML_SUB_DECODE = 1 << 1, 96 AML_POS_DECODE = 0 97 } AmlDecode; 98 99 typedef enum { 100 AML_MAX_FIXED = 1 << 3, 101 AML_MAX_NOT_FIXED = 0, 102 } AmlMaxFixed; 103 104 typedef enum { 105 AML_MIN_FIXED = 1 << 2, 106 AML_MIN_NOT_FIXED = 0 107 } AmlMinFixed; 108 109 /* 110 * ACPI 1.0b: Table 6-26 I/O Resource Flag (Resource Type = 1) Definitions 111 * _RNG field definition 112 */ 113 typedef enum { 114 AML_ISA_ONLY = 1, 115 AML_NON_ISA_ONLY = 2, 116 AML_ENTIRE_RANGE = 3, 117 } AmlISARanges; 118 119 /* 120 * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions 121 * _MEM field definition 122 */ 123 typedef enum { 124 AML_NON_CACHEABLE = 0, 125 AML_CACHEABLE = 1, 126 AML_WRITE_COMBINING = 2, 127 AML_PREFETCHABLE = 3, 128 } AmlCacheable; 129 130 /* 131 * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions 132 * _RW field definition 133 */ 134 typedef enum { 135 AML_READ_ONLY = 0, 136 AML_READ_WRITE = 1, 137 } AmlReadAndWrite; 138 139 /* 140 * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition 141 * Interrupt Vector Flags Bits[0] Consumer/Producer 142 */ 143 typedef enum { 144 AML_CONSUMER_PRODUCER = 0, 145 AML_CONSUMER = 1, 146 } AmlConsumerAndProducer; 147 148 /* 149 * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition 150 * _HE field definition 151 */ 152 typedef enum { 153 AML_LEVEL = 0, 154 AML_EDGE = 1, 155 } AmlLevelAndEdge; 156 157 /* 158 * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition 159 * _LL field definition 160 */ 161 typedef enum { 162 AML_ACTIVE_HIGH = 0, 163 AML_ACTIVE_LOW = 1, 164 } AmlActiveHighAndLow; 165 166 /* 167 * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition 168 * _SHR field definition 169 */ 170 typedef enum { 171 AML_EXCLUSIVE = 0, 172 AML_SHARED = 1, 173 AML_EXCLUSIVE_AND_WAKE = 2, 174 AML_SHARED_AND_WAKE = 3, 175 } AmlShared; 176 177 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: MethodFlags */ 178 typedef enum { 179 AML_NOTSERIALIZED = 0, 180 AML_SERIALIZED = 1, 181 } AmlSerializeFlag; 182 183 /* 184 * ACPI 5.0: Table 6-189 GPIO Connection Descriptor Definition 185 * GPIO Connection Type 186 */ 187 typedef enum { 188 AML_INTERRUPT_CONNECTION = 0, 189 AML_IO_CONNECTION = 1, 190 } AmlGpioConnectionType; 191 192 /* 193 * ACPI 5.0: Table 6-189 GPIO Connection Descriptor Definition 194 * _PPI field definition 195 */ 196 typedef enum { 197 AML_PULL_DEFAULT = 0, 198 AML_PULL_UP = 1, 199 AML_PULL_DOWN = 2, 200 AML_PULL_NONE = 3, 201 } AmlPinConfig; 202 203 typedef 204 struct AcpiBuildTables { 205 GArray *table_data; 206 GArray *rsdp; 207 GArray *tcpalog; 208 GArray *linker; 209 } AcpiBuildTables; 210 211 /** 212 * init_aml_allocator: 213 * 214 * Called for initializing API allocator which allow to use 215 * AML API. 216 * Returns: toplevel container which accumulates all other 217 * AML elements for a table. 218 */ 219 Aml *init_aml_allocator(void); 220 221 /** 222 * free_aml_allocator: 223 * 224 * Releases all elements used by AML API, frees associated memory 225 * and invalidates AML allocator. After this call @init_aml_allocator 226 * should be called again if AML API is to be used again. 227 */ 228 void free_aml_allocator(void); 229 230 /** 231 * aml_append: 232 * @parent_ctx: context to which @child element is added 233 * @child: element that is copied into @parent_ctx context 234 * 235 * Joins Aml elements together and helps to construct AML tables 236 * Examle of usage: 237 * Aml *table = aml_def_block("SSDT", ...); 238 * Aml *sb = aml_scope("\\_SB"); 239 * Aml *dev = aml_device("PCI0"); 240 * 241 * aml_append(dev, aml_name_decl("HID", aml_eisaid("PNP0A03"))); 242 * aml_append(sb, dev); 243 * aml_append(table, sb); 244 */ 245 void aml_append(Aml *parent_ctx, Aml *child); 246 247 /* non block AML object primitives */ 248 Aml *aml_name(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 249 Aml *aml_name_decl(const char *name, Aml *val); 250 Aml *aml_return(Aml *val); 251 Aml *aml_int(const uint64_t val); 252 Aml *aml_arg(int pos); 253 Aml *aml_to_integer(Aml *arg); 254 Aml *aml_to_hexstring(Aml *src, Aml *dst); 255 Aml *aml_to_buffer(Aml *src, Aml *dst); 256 Aml *aml_store(Aml *val, Aml *target); 257 Aml *aml_and(Aml *arg1, Aml *arg2, Aml *dst); 258 Aml *aml_or(Aml *arg1, Aml *arg2, Aml *dst); 259 Aml *aml_lor(Aml *arg1, Aml *arg2); 260 Aml *aml_shiftleft(Aml *arg1, Aml *count); 261 Aml *aml_shiftright(Aml *arg1, Aml *count, Aml *dst); 262 Aml *aml_lless(Aml *arg1, Aml *arg2); 263 Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst); 264 Aml *aml_subtract(Aml *arg1, Aml *arg2, Aml *dst); 265 Aml *aml_increment(Aml *arg); 266 Aml *aml_decrement(Aml *arg); 267 Aml *aml_index(Aml *arg1, Aml *idx); 268 Aml *aml_notify(Aml *arg1, Aml *arg2); 269 Aml *aml_call0(const char *method); 270 Aml *aml_call1(const char *method, Aml *arg1); 271 Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2); 272 Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3); 273 Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4); 274 Aml *aml_gpio_int(AmlConsumerAndProducer con_and_pro, 275 AmlLevelAndEdge edge_level, 276 AmlActiveHighAndLow active_level, AmlShared shared, 277 AmlPinConfig pin_config, uint16_t debounce_timeout, 278 const uint32_t pin_list[], uint32_t pin_count, 279 const char *resource_source_name, 280 const uint8_t *vendor_data, uint16_t vendor_data_len); 281 Aml *aml_memory32_fixed(uint32_t addr, uint32_t size, 282 AmlReadAndWrite read_and_write); 283 Aml *aml_interrupt(AmlConsumerAndProducer con_and_pro, 284 AmlLevelAndEdge level_and_edge, 285 AmlActiveHighAndLow high_and_low, AmlShared shared, 286 uint32_t *irq_list, uint8_t irq_count); 287 Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, 288 uint8_t aln, uint8_t len); 289 Aml *aml_operation_region(const char *name, AmlRegionSpace rs, 290 uint32_t offset, uint32_t len); 291 Aml *aml_irq_no_flags(uint8_t irq); 292 Aml *aml_named_field(const char *name, unsigned length); 293 Aml *aml_reserved_field(unsigned length); 294 Aml *aml_local(int num); 295 Aml *aml_string(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 296 Aml *aml_lnot(Aml *arg); 297 Aml *aml_equal(Aml *arg1, Aml *arg2); 298 Aml *aml_lgreater(Aml *arg1, Aml *arg2); 299 Aml *aml_lgreater_equal(Aml *arg1, Aml *arg2); 300 Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len, 301 const char *name_format, ...) GCC_FMT_ATTR(4, 5); 302 Aml *aml_eisaid(const char *str); 303 Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, 304 AmlDecode dec, uint16_t addr_gran, 305 uint16_t addr_min, uint16_t addr_max, 306 uint16_t addr_trans, uint16_t len); 307 Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, 308 AmlDecode dec, AmlISARanges isa_ranges, 309 uint16_t addr_gran, uint16_t addr_min, 310 uint16_t addr_max, uint16_t addr_trans, 311 uint16_t len); 312 Aml *aml_dword_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, 313 AmlDecode dec, AmlISARanges isa_ranges, 314 uint32_t addr_gran, uint32_t addr_min, 315 uint32_t addr_max, uint32_t addr_trans, 316 uint32_t len); 317 Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed, 318 AmlMaxFixed max_fixed, AmlCacheable cacheable, 319 AmlReadAndWrite read_and_write, 320 uint32_t addr_gran, uint32_t addr_min, 321 uint32_t addr_max, uint32_t addr_trans, 322 uint32_t len); 323 Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed, 324 AmlMaxFixed max_fixed, AmlCacheable cacheable, 325 AmlReadAndWrite read_and_write, 326 uint64_t addr_gran, uint64_t addr_min, 327 uint64_t addr_max, uint64_t addr_trans, 328 uint64_t len); 329 Aml *aml_dma(AmlDmaType typ, AmlDmaBusMaster bm, AmlTransferSize sz, 330 uint8_t channel); 331 Aml *aml_sleep(uint64_t msec); 332 333 /* Block AML object primitives */ 334 Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 335 Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 336 Aml *aml_method(const char *name, int arg_count, AmlSerializeFlag sflag); 337 Aml *aml_if(Aml *predicate); 338 Aml *aml_else(void); 339 Aml *aml_while(Aml *predicate); 340 Aml *aml_package(uint8_t num_elements); 341 Aml *aml_buffer(int buffer_size, uint8_t *byte_list); 342 Aml *aml_resource_template(void); 343 Aml *aml_field(const char *name, AmlAccessType type, AmlLockRule lock, 344 AmlUpdateRule rule); 345 Aml *aml_mutex(const char *name, uint8_t sync_level); 346 Aml *aml_acquire(Aml *mutex, uint16_t timeout); 347 Aml *aml_release(Aml *mutex); 348 Aml *aml_alias(const char *source_object, const char *alias_object); 349 Aml *aml_create_dword_field(Aml *srcbuf, Aml *index, const char *name); 350 Aml *aml_create_qword_field(Aml *srcbuf, Aml *index, const char *name); 351 Aml *aml_varpackage(uint32_t num_elements); 352 Aml *aml_touuid(const char *uuid); 353 Aml *aml_unicode(const char *str); 354 Aml *aml_derefof(Aml *arg); 355 Aml *aml_sizeof(Aml *arg); 356 357 void 358 build_header(GArray *linker, GArray *table_data, 359 AcpiTableHeader *h, const char *sig, int len, uint8_t rev, 360 const char *oem_table_id); 361 void *acpi_data_push(GArray *table_data, unsigned size); 362 unsigned acpi_data_len(GArray *table); 363 void acpi_add_table(GArray *table_offsets, GArray *table_data); 364 void acpi_build_tables_init(AcpiBuildTables *tables); 365 void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre); 366 void 367 build_rsdt(GArray *table_data, GArray *linker, GArray *table_offsets); 368 369 #endif 370