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