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_APPNAME "Bochs" 13 #define ACPI_BUILD_APPNAME6 "BOCHS " 14 #define ACPI_BUILD_APPNAME4 "BXPC" 15 16 #define ACPI_BUILD_TABLE_FILE "etc/acpi/tables" 17 #define ACPI_BUILD_RSDP_FILE "etc/acpi/rsdp" 18 #define ACPI_BUILD_TPMLOG_FILE "etc/tpm/log" 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_decode10 = 0, 40 aml_decode16 = 1, 41 } AmlIODecode; 42 43 typedef enum { 44 aml_any_acc = 0, 45 aml_byte_acc = 1, 46 aml_word_acc = 2, 47 aml_dword_acc = 3, 48 aml_qword_acc = 4, 49 aml_buffer_acc = 5, 50 } AmlFieldFlags; 51 52 typedef enum { 53 aml_system_memory = 0x00, 54 aml_system_io = 0x01, 55 } AmlRegionSpace; 56 57 typedef enum { 58 aml_memory_range = 0, 59 aml_io_range = 1, 60 aml_bus_number_range = 2, 61 } AmlResourceType; 62 63 typedef enum { 64 aml_sub_decode = 1 << 1, 65 aml_pos_decode = 0 66 } AmlDecode; 67 68 typedef enum { 69 aml_max_fixed = 1 << 3, 70 aml_max_not_fixed = 0, 71 } AmlMaxFixed; 72 73 typedef enum { 74 aml_min_fixed = 1 << 2, 75 aml_min_not_fixed = 0 76 } AmlMinFixed; 77 78 /* 79 * ACPI 1.0b: Table 6-26 I/O Resource Flag (Resource Type = 1) Definitions 80 * _RNG field definition 81 */ 82 typedef enum { 83 aml_isa_only = 1, 84 aml_non_isa_only = 2, 85 aml_entire_range = 3, 86 } AmlISARanges; 87 88 /* 89 * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions 90 * _MEM field definition 91 */ 92 typedef enum { 93 aml_non_cacheable = 0, 94 aml_cacheable = 1, 95 aml_write_combining = 2, 96 aml_prefetchable = 3, 97 } AmlCacheble; 98 99 /* 100 * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions 101 * _RW field definition 102 */ 103 typedef enum { 104 aml_ReadOnly = 0, 105 aml_ReadWrite = 1, 106 } AmlReadAndWrite; 107 108 typedef 109 struct AcpiBuildTables { 110 GArray *table_data; 111 GArray *rsdp; 112 GArray *tcpalog; 113 GArray *linker; 114 } AcpiBuildTables; 115 116 /** 117 * init_aml_allocator: 118 * 119 * Called for initializing API allocator which allow to use 120 * AML API. 121 * Returns: toplevel container which accumulates all other 122 * AML elements for a table. 123 */ 124 Aml *init_aml_allocator(void); 125 126 /** 127 * free_aml_allocator: 128 * 129 * Releases all elements used by AML API, frees associated memory 130 * and invalidates AML allocator. After this call @init_aml_allocator 131 * should be called again if AML API is to be used again. 132 */ 133 void free_aml_allocator(void); 134 135 /** 136 * aml_append: 137 * @parent_ctx: context to which @child element is added 138 * @child: element that is copied into @parent_ctx context 139 * 140 * Joins Aml elements together and helps to construct AML tables 141 * Examle of usage: 142 * Aml *table = aml_def_block("SSDT", ...); 143 * Aml *sb = aml_scope("\_SB"); 144 * Aml *dev = aml_device("PCI0"); 145 * 146 * aml_append(dev, aml_name_decl("HID", aml_eisaid("PNP0A03"))); 147 * aml_append(sb, dev); 148 * aml_append(table, sb); 149 */ 150 void aml_append(Aml *parent_ctx, Aml *child); 151 152 /* non block AML object primitives */ 153 Aml *aml_name(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 154 Aml *aml_name_decl(const char *name, Aml *val); 155 Aml *aml_return(Aml *val); 156 Aml *aml_int(const uint64_t val); 157 Aml *aml_arg(int pos); 158 Aml *aml_store(Aml *val, Aml *target); 159 Aml *aml_and(Aml *arg1, Aml *arg2); 160 Aml *aml_notify(Aml *arg1, Aml *arg2); 161 Aml *aml_call1(const char *method, Aml *arg1); 162 Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2); 163 Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3); 164 Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4); 165 Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, 166 uint8_t aln, uint8_t len); 167 Aml *aml_operation_region(const char *name, AmlRegionSpace rs, 168 uint32_t offset, uint32_t len); 169 Aml *aml_irq_no_flags(uint8_t irq); 170 Aml *aml_named_field(const char *name, unsigned length); 171 Aml *aml_reserved_field(unsigned length); 172 Aml *aml_local(int num); 173 Aml *aml_string(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 174 Aml *aml_equal(Aml *arg1, Aml *arg2); 175 Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len, 176 const char *name_format, ...) GCC_FMT_ATTR(4, 5); 177 Aml *aml_eisaid(const char *str); 178 Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, 179 AmlDecode dec, uint16_t addr_gran, 180 uint16_t addr_min, uint16_t addr_max, 181 uint16_t addr_trans, uint16_t len); 182 Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, 183 AmlDecode dec, AmlISARanges isa_ranges, 184 uint16_t addr_gran, uint16_t addr_min, 185 uint16_t addr_max, uint16_t addr_trans, 186 uint16_t len); 187 Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed, 188 AmlMaxFixed max_fixed, AmlCacheble cacheable, 189 AmlReadAndWrite read_and_write, 190 uint32_t addr_gran, uint32_t addr_min, 191 uint32_t addr_max, uint32_t addr_trans, 192 uint32_t len); 193 Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed, 194 AmlMaxFixed max_fixed, AmlCacheble cacheable, 195 AmlReadAndWrite read_and_write, 196 uint64_t addr_gran, uint64_t addr_min, 197 uint64_t addr_max, uint64_t addr_trans, 198 uint64_t len); 199 200 /* Block AML object primitives */ 201 Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 202 Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 203 Aml *aml_method(const char *name, int arg_count); 204 Aml *aml_if(Aml *predicate); 205 Aml *aml_package(uint8_t num_elements); 206 Aml *aml_buffer(void); 207 Aml *aml_resource_template(void); 208 Aml *aml_field(const char *name, AmlFieldFlags flags); 209 Aml *aml_varpackage(uint32_t num_elements); 210 211 void 212 build_header(GArray *linker, GArray *table_data, 213 AcpiTableHeader *h, const char *sig, int len, uint8_t rev); 214 void *acpi_data_push(GArray *table_data, unsigned size); 215 unsigned acpi_data_len(GArray *table); 216 void acpi_add_table(GArray *table_offsets, GArray *table_data); 217 void acpi_build_tables_init(AcpiBuildTables *tables); 218 void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre); 219 220 #endif 221