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 typedef 152 struct AcpiBuildTables { 153 GArray *table_data; 154 GArray *rsdp; 155 GArray *tcpalog; 156 GArray *linker; 157 } AcpiBuildTables; 158 159 /** 160 * init_aml_allocator: 161 * 162 * Called for initializing API allocator which allow to use 163 * AML API. 164 * Returns: toplevel container which accumulates all other 165 * AML elements for a table. 166 */ 167 Aml *init_aml_allocator(void); 168 169 /** 170 * free_aml_allocator: 171 * 172 * Releases all elements used by AML API, frees associated memory 173 * and invalidates AML allocator. After this call @init_aml_allocator 174 * should be called again if AML API is to be used again. 175 */ 176 void free_aml_allocator(void); 177 178 /** 179 * aml_append: 180 * @parent_ctx: context to which @child element is added 181 * @child: element that is copied into @parent_ctx context 182 * 183 * Joins Aml elements together and helps to construct AML tables 184 * Examle of usage: 185 * Aml *table = aml_def_block("SSDT", ...); 186 * Aml *sb = aml_scope("\\_SB"); 187 * Aml *dev = aml_device("PCI0"); 188 * 189 * aml_append(dev, aml_name_decl("HID", aml_eisaid("PNP0A03"))); 190 * aml_append(sb, dev); 191 * aml_append(table, sb); 192 */ 193 void aml_append(Aml *parent_ctx, Aml *child); 194 195 /* non block AML object primitives */ 196 Aml *aml_name(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 197 Aml *aml_name_decl(const char *name, Aml *val); 198 Aml *aml_return(Aml *val); 199 Aml *aml_int(const uint64_t val); 200 Aml *aml_arg(int pos); 201 Aml *aml_store(Aml *val, Aml *target); 202 Aml *aml_and(Aml *arg1, Aml *arg2); 203 Aml *aml_or(Aml *arg1, Aml *arg2); 204 Aml *aml_shiftleft(Aml *arg1, Aml *count); 205 Aml *aml_shiftright(Aml *arg1, Aml *count); 206 Aml *aml_lless(Aml *arg1, Aml *arg2); 207 Aml *aml_add(Aml *arg1, Aml *arg2); 208 Aml *aml_increment(Aml *arg); 209 Aml *aml_index(Aml *arg1, Aml *idx); 210 Aml *aml_notify(Aml *arg1, Aml *arg2); 211 Aml *aml_call1(const char *method, Aml *arg1); 212 Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2); 213 Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3); 214 Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4); 215 Aml *aml_memory32_fixed(uint32_t addr, uint32_t size, 216 AmlReadAndWrite read_and_write); 217 Aml *aml_interrupt(AmlConsumerAndProducer con_and_pro, 218 AmlLevelAndEdge level_and_edge, 219 AmlActiveHighAndLow high_and_low, AmlShared shared, 220 uint32_t irq); 221 Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, 222 uint8_t aln, uint8_t len); 223 Aml *aml_operation_region(const char *name, AmlRegionSpace rs, 224 uint32_t offset, uint32_t len); 225 Aml *aml_irq_no_flags(uint8_t irq); 226 Aml *aml_named_field(const char *name, unsigned length); 227 Aml *aml_reserved_field(unsigned length); 228 Aml *aml_local(int num); 229 Aml *aml_string(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 230 Aml *aml_lnot(Aml *arg); 231 Aml *aml_equal(Aml *arg1, Aml *arg2); 232 Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len, 233 const char *name_format, ...) GCC_FMT_ATTR(4, 5); 234 Aml *aml_eisaid(const char *str); 235 Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, 236 AmlDecode dec, uint16_t addr_gran, 237 uint16_t addr_min, uint16_t addr_max, 238 uint16_t addr_trans, uint16_t len); 239 Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, 240 AmlDecode dec, AmlISARanges isa_ranges, 241 uint16_t addr_gran, uint16_t addr_min, 242 uint16_t addr_max, uint16_t addr_trans, 243 uint16_t len); 244 Aml *aml_dword_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, 245 AmlDecode dec, AmlISARanges isa_ranges, 246 uint32_t addr_gran, uint32_t addr_min, 247 uint32_t addr_max, uint32_t addr_trans, 248 uint32_t len); 249 Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed, 250 AmlMaxFixed max_fixed, AmlCacheable cacheable, 251 AmlReadAndWrite read_and_write, 252 uint32_t addr_gran, uint32_t addr_min, 253 uint32_t addr_max, uint32_t addr_trans, 254 uint32_t len); 255 Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed, 256 AmlMaxFixed max_fixed, AmlCacheable cacheable, 257 AmlReadAndWrite read_and_write, 258 uint64_t addr_gran, uint64_t addr_min, 259 uint64_t addr_max, uint64_t addr_trans, 260 uint64_t len); 261 262 /* Block AML object primitives */ 263 Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 264 Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 265 Aml *aml_method(const char *name, int arg_count); 266 Aml *aml_if(Aml *predicate); 267 Aml *aml_else(void); 268 Aml *aml_while(Aml *predicate); 269 Aml *aml_package(uint8_t num_elements); 270 Aml *aml_buffer(int buffer_size, uint8_t *byte_list); 271 Aml *aml_resource_template(void); 272 Aml *aml_field(const char *name, AmlAccessType type, AmlUpdateRule rule); 273 Aml *aml_create_dword_field(Aml *srcbuf, Aml *index, const char *name); 274 Aml *aml_varpackage(uint32_t num_elements); 275 Aml *aml_touuid(const char *uuid); 276 Aml *aml_unicode(const char *str); 277 278 void 279 build_header(GArray *linker, GArray *table_data, 280 AcpiTableHeader *h, const char *sig, int len, uint8_t rev); 281 void *acpi_data_push(GArray *table_data, unsigned size); 282 unsigned acpi_data_len(GArray *table); 283 void acpi_add_table(GArray *table_offsets, GArray *table_data); 284 void acpi_build_tables_init(AcpiBuildTables *tables); 285 void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre); 286 void 287 build_rsdt(GArray *table_data, GArray *linker, GArray *table_offsets); 288 289 #endif 290