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