119934e0eSIgor Mammedov #ifndef HW_ACPI_GEN_UTILS_H 219934e0eSIgor Mammedov #define HW_ACPI_GEN_UTILS_H 319934e0eSIgor Mammedov 419934e0eSIgor Mammedov #include <stdint.h> 519934e0eSIgor Mammedov #include <glib.h> 619934e0eSIgor Mammedov #include "qemu/compiler.h" 7658c2718SShannon Zhao #include "hw/acpi/acpi-defs.h" 8658c2718SShannon Zhao 9658c2718SShannon Zhao /* Reserve RAM space for tables: add another order of magnitude. */ 10658c2718SShannon Zhao #define ACPI_BUILD_TABLE_MAX_SIZE 0x200000 11658c2718SShannon Zhao 12658c2718SShannon Zhao #define ACPI_BUILD_APPNAME "Bochs" 13658c2718SShannon Zhao #define ACPI_BUILD_APPNAME6 "BOCHS " 14658c2718SShannon Zhao #define ACPI_BUILD_APPNAME4 "BXPC" 15658c2718SShannon Zhao 16658c2718SShannon Zhao #define ACPI_BUILD_TABLE_FILE "etc/acpi/tables" 17658c2718SShannon Zhao #define ACPI_BUILD_RSDP_FILE "etc/acpi/rsdp" 18658c2718SShannon Zhao #define ACPI_BUILD_TPMLOG_FILE "etc/tpm/log" 1919934e0eSIgor Mammedov 200f2707e4SIgor Mammedov typedef enum { 210f2707e4SIgor Mammedov AML_NO_OPCODE = 0,/* has only data */ 220f2707e4SIgor Mammedov AML_OPCODE, /* has opcode optionally followed by data */ 230f2707e4SIgor Mammedov AML_PACKAGE, /* has opcode and uses PkgLength for its length */ 2456521fb8SMichael S. Tsirkin AML_EXT_PACKAGE, /* Same as AML_PACKAGE but also has 'ExOpPrefix' */ 250f2707e4SIgor Mammedov AML_BUFFER, /* data encoded as 'DefBuffer' */ 260f2707e4SIgor Mammedov AML_RES_TEMPLATE, /* encoded as ResourceTemplate macro */ 270f2707e4SIgor Mammedov } AmlBlockFlags; 280f2707e4SIgor Mammedov 290f2707e4SIgor Mammedov struct Aml { 300f2707e4SIgor Mammedov GArray *buf; 310f2707e4SIgor Mammedov 320f2707e4SIgor Mammedov /*< private >*/ 330f2707e4SIgor Mammedov uint8_t op; 340f2707e4SIgor Mammedov AmlBlockFlags block_flags; 350f2707e4SIgor Mammedov }; 360f2707e4SIgor Mammedov typedef struct Aml Aml; 370f2707e4SIgor Mammedov 3852fa397cSIgor Mammedov typedef enum { 39ff80dc7fSShannon Zhao AML_DECODE10 = 0, 40ff80dc7fSShannon Zhao AML_DECODE16 = 1, 4152fa397cSIgor Mammedov } AmlIODecode; 4252fa397cSIgor Mammedov 4331127938SIgor Mammedov typedef enum { 44ff80dc7fSShannon Zhao AML_ANY_ACC = 0, 45ff80dc7fSShannon Zhao AML_BYTE_ACC = 1, 46ff80dc7fSShannon Zhao AML_WORD_ACC = 2, 47ff80dc7fSShannon Zhao AML_DWORD_ACC = 3, 48ff80dc7fSShannon Zhao AML_QWORD_ACC = 4, 49ff80dc7fSShannon Zhao AML_BUFFER_ACC = 5, 50af509897SZhu Guihua } AmlAccessType; 51af509897SZhu Guihua 52af509897SZhu Guihua typedef enum { 53ff80dc7fSShannon Zhao AML_PRESERVE = 0, 54ff80dc7fSShannon Zhao AML_WRITE_AS_ONES = 1, 55ff80dc7fSShannon Zhao AML_WRITE_AS_ZEROS = 2, 56af509897SZhu Guihua } AmlUpdateRule; 57214ae59fSIgor Mammedov 58214ae59fSIgor Mammedov typedef enum { 59ff80dc7fSShannon Zhao AML_SYSTEM_MEMORY = 0X00, 60ff80dc7fSShannon Zhao AML_SYSTEM_IO = 0X01, 6131127938SIgor Mammedov } AmlRegionSpace; 6231127938SIgor Mammedov 636ece7053SIgor Mammedov typedef enum { 64ff80dc7fSShannon Zhao AML_MEMORY_RANGE = 0, 65ff80dc7fSShannon Zhao AML_IO_RANGE = 1, 66ff80dc7fSShannon Zhao AML_BUS_NUMBER_RANGE = 2, 676ece7053SIgor Mammedov } AmlResourceType; 686ece7053SIgor Mammedov 696ece7053SIgor Mammedov typedef enum { 70ff80dc7fSShannon Zhao AML_SUB_DECODE = 1 << 1, 71ff80dc7fSShannon Zhao AML_POS_DECODE = 0 726ece7053SIgor Mammedov } AmlDecode; 736ece7053SIgor Mammedov 746ece7053SIgor Mammedov typedef enum { 75ff80dc7fSShannon Zhao AML_MAX_FIXED = 1 << 3, 76ff80dc7fSShannon Zhao AML_MAX_NOT_FIXED = 0, 776ece7053SIgor Mammedov } AmlMaxFixed; 786ece7053SIgor Mammedov 796ece7053SIgor Mammedov typedef enum { 80ff80dc7fSShannon Zhao AML_MIN_FIXED = 1 << 2, 81ff80dc7fSShannon Zhao AML_MIN_NOT_FIXED = 0 826ece7053SIgor Mammedov } AmlMinFixed; 836ece7053SIgor Mammedov 846ece7053SIgor Mammedov /* 856ece7053SIgor Mammedov * ACPI 1.0b: Table 6-26 I/O Resource Flag (Resource Type = 1) Definitions 866ece7053SIgor Mammedov * _RNG field definition 876ece7053SIgor Mammedov */ 886ece7053SIgor Mammedov typedef enum { 89ff80dc7fSShannon Zhao AML_ISA_ONLY = 1, 90ff80dc7fSShannon Zhao AML_NON_ISA_ONLY = 2, 91ff80dc7fSShannon Zhao AML_ENTIRE_RANGE = 3, 926ece7053SIgor Mammedov } AmlISARanges; 936ece7053SIgor Mammedov 946ece7053SIgor Mammedov /* 956ece7053SIgor Mammedov * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions 966ece7053SIgor Mammedov * _MEM field definition 976ece7053SIgor Mammedov */ 986ece7053SIgor Mammedov typedef enum { 99ff80dc7fSShannon Zhao AML_NON_CACHEABLE = 0, 100ff80dc7fSShannon Zhao AML_CACHEABLE = 1, 101ff80dc7fSShannon Zhao AML_WRITE_COMBINING = 2, 102ff80dc7fSShannon Zhao AML_PREFETCHABLE = 3, 103ff80dc7fSShannon Zhao } AmlCacheable; 1046ece7053SIgor Mammedov 1056ece7053SIgor Mammedov /* 1066ece7053SIgor Mammedov * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions 1076ece7053SIgor Mammedov * _RW field definition 1086ece7053SIgor Mammedov */ 1096ece7053SIgor Mammedov typedef enum { 110ff80dc7fSShannon Zhao AML_READ_ONLY = 0, 111ff80dc7fSShannon Zhao AML_READ_WRITE = 1, 1126ece7053SIgor Mammedov } AmlReadAndWrite; 1136ece7053SIgor Mammedov 114205d1d1cSShannon Zhao /* 115205d1d1cSShannon Zhao * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition 116205d1d1cSShannon Zhao * Interrupt Vector Flags Bits[0] Consumer/Producer 117205d1d1cSShannon Zhao */ 118205d1d1cSShannon Zhao typedef enum { 119205d1d1cSShannon Zhao AML_CONSUMER_PRODUCER = 0, 120205d1d1cSShannon Zhao AML_CONSUMER = 1, 121205d1d1cSShannon Zhao } AmlConsumerAndProducer; 122205d1d1cSShannon Zhao 123205d1d1cSShannon Zhao /* 124205d1d1cSShannon Zhao * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition 125205d1d1cSShannon Zhao * _HE field definition 126205d1d1cSShannon Zhao */ 127205d1d1cSShannon Zhao typedef enum { 128205d1d1cSShannon Zhao AML_LEVEL = 0, 129205d1d1cSShannon Zhao AML_EDGE = 1, 130205d1d1cSShannon Zhao } AmlLevelAndEdge; 131205d1d1cSShannon Zhao 132205d1d1cSShannon Zhao /* 133205d1d1cSShannon Zhao * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition 134205d1d1cSShannon Zhao * _LL field definition 135205d1d1cSShannon Zhao */ 136205d1d1cSShannon Zhao typedef enum { 137205d1d1cSShannon Zhao AML_ACTIVE_HIGH = 0, 138205d1d1cSShannon Zhao AML_ACTIVE_LOW = 1, 139205d1d1cSShannon Zhao } AmlActiveHighAndLow; 140205d1d1cSShannon Zhao 141205d1d1cSShannon Zhao /* 142205d1d1cSShannon Zhao * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition 143205d1d1cSShannon Zhao * _SHR field definition 144205d1d1cSShannon Zhao */ 145205d1d1cSShannon Zhao typedef enum { 146205d1d1cSShannon Zhao AML_EXCLUSIVE = 0, 147205d1d1cSShannon Zhao AML_SHARED = 1, 148205d1d1cSShannon Zhao AML_EXCLUSIVE_AND_WAKE = 2, 149205d1d1cSShannon Zhao AML_SHARED_AND_WAKE = 3, 150205d1d1cSShannon Zhao } AmlShared; 151205d1d1cSShannon Zhao 152658c2718SShannon Zhao typedef 153658c2718SShannon Zhao struct AcpiBuildTables { 154658c2718SShannon Zhao GArray *table_data; 155658c2718SShannon Zhao GArray *rsdp; 156658c2718SShannon Zhao GArray *tcpalog; 157658c2718SShannon Zhao GArray *linker; 158658c2718SShannon Zhao } AcpiBuildTables; 159658c2718SShannon Zhao 1600f2707e4SIgor Mammedov /** 1610f2707e4SIgor Mammedov * init_aml_allocator: 1620f2707e4SIgor Mammedov * 1630f2707e4SIgor Mammedov * Called for initializing API allocator which allow to use 1640f2707e4SIgor Mammedov * AML API. 1650f2707e4SIgor Mammedov * Returns: toplevel container which accumulates all other 1660f2707e4SIgor Mammedov * AML elements for a table. 1670f2707e4SIgor Mammedov */ 1680f2707e4SIgor Mammedov Aml *init_aml_allocator(void); 1690f2707e4SIgor Mammedov 1700f2707e4SIgor Mammedov /** 1710f2707e4SIgor Mammedov * free_aml_allocator: 1720f2707e4SIgor Mammedov * 1730f2707e4SIgor Mammedov * Releases all elements used by AML API, frees associated memory 1740f2707e4SIgor Mammedov * and invalidates AML allocator. After this call @init_aml_allocator 1750f2707e4SIgor Mammedov * should be called again if AML API is to be used again. 1760f2707e4SIgor Mammedov */ 1770f2707e4SIgor Mammedov void free_aml_allocator(void); 1780f2707e4SIgor Mammedov 1790f2707e4SIgor Mammedov /** 1800f2707e4SIgor Mammedov * aml_append: 1810f2707e4SIgor Mammedov * @parent_ctx: context to which @child element is added 1820f2707e4SIgor Mammedov * @child: element that is copied into @parent_ctx context 1830f2707e4SIgor Mammedov * 1840f2707e4SIgor Mammedov * Joins Aml elements together and helps to construct AML tables 1850f2707e4SIgor Mammedov * Examle of usage: 1860f2707e4SIgor Mammedov * Aml *table = aml_def_block("SSDT", ...); 1877824df38SGal Hammer * Aml *sb = aml_scope("\\_SB"); 1880f2707e4SIgor Mammedov * Aml *dev = aml_device("PCI0"); 1890f2707e4SIgor Mammedov * 1900f2707e4SIgor Mammedov * aml_append(dev, aml_name_decl("HID", aml_eisaid("PNP0A03"))); 1910f2707e4SIgor Mammedov * aml_append(sb, dev); 1920f2707e4SIgor Mammedov * aml_append(table, sb); 1930f2707e4SIgor Mammedov */ 1940f2707e4SIgor Mammedov void aml_append(Aml *parent_ctx, Aml *child); 1950f2707e4SIgor Mammedov 1963c054bd5SIgor Mammedov /* non block AML object primitives */ 1973c054bd5SIgor Mammedov Aml *aml_name(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 1983c054bd5SIgor Mammedov Aml *aml_name_decl(const char *name, Aml *val); 199b25af5adSIgor Mammedov Aml *aml_return(Aml *val); 200295a515dSIgor Mammedov Aml *aml_int(const uint64_t val); 2017193f3a6SIgor Mammedov Aml *aml_arg(int pos); 202c263b3f7SIgor Mammedov Aml *aml_store(Aml *val, Aml *target); 203926f5aaeSIgor Mammedov Aml *aml_and(Aml *arg1, Aml *arg2); 20434189453SIgor Mammedov Aml *aml_notify(Aml *arg1, Aml *arg2); 2053f3992b7SIgor Mammedov Aml *aml_call1(const char *method, Aml *arg1); 2063f3992b7SIgor Mammedov Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2); 2073f3992b7SIgor Mammedov Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3); 2083f3992b7SIgor Mammedov Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4); 209dc17ab1dSShannon Zhao Aml *aml_memory32_fixed(uint32_t addr, uint32_t size, 210dc17ab1dSShannon Zhao AmlReadAndWrite read_and_write); 211205d1d1cSShannon Zhao Aml *aml_interrupt(AmlConsumerAndProducer con_and_pro, 212205d1d1cSShannon Zhao AmlLevelAndEdge level_and_edge, 213205d1d1cSShannon Zhao AmlActiveHighAndLow high_and_low, AmlShared shared, 214205d1d1cSShannon Zhao uint32_t irq); 21552fa397cSIgor Mammedov Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, 21652fa397cSIgor Mammedov uint8_t aln, uint8_t len); 21731127938SIgor Mammedov Aml *aml_operation_region(const char *name, AmlRegionSpace rs, 21831127938SIgor Mammedov uint32_t offset, uint32_t len); 21970560453SIgor Mammedov Aml *aml_irq_no_flags(uint8_t irq); 220214ae59fSIgor Mammedov Aml *aml_named_field(const char *name, unsigned length); 221e2ea299bSIgor Mammedov Aml *aml_reserved_field(unsigned length); 222b8a5d689SIgor Mammedov Aml *aml_local(int num); 223d5e5830fSIgor Mammedov Aml *aml_string(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 22415e44e56SIgor Mammedov Aml *aml_equal(Aml *arg1, Aml *arg2); 2253dd15643SIgor Mammedov Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len, 2263dd15643SIgor Mammedov const char *name_format, ...) GCC_FMT_ATTR(4, 5); 227a7891dacSIgor Mammedov Aml *aml_eisaid(const char *str); 2286ece7053SIgor Mammedov Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, 2296ece7053SIgor Mammedov AmlDecode dec, uint16_t addr_gran, 2306ece7053SIgor Mammedov uint16_t addr_min, uint16_t addr_max, 2316ece7053SIgor Mammedov uint16_t addr_trans, uint16_t len); 2326ece7053SIgor Mammedov Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, 2336ece7053SIgor Mammedov AmlDecode dec, AmlISARanges isa_ranges, 2346ece7053SIgor Mammedov uint16_t addr_gran, uint16_t addr_min, 2356ece7053SIgor Mammedov uint16_t addr_max, uint16_t addr_trans, 2366ece7053SIgor Mammedov uint16_t len); 2376ece7053SIgor Mammedov Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed, 238ff80dc7fSShannon Zhao AmlMaxFixed max_fixed, AmlCacheable cacheable, 2396ece7053SIgor Mammedov AmlReadAndWrite read_and_write, 2406ece7053SIgor Mammedov uint32_t addr_gran, uint32_t addr_min, 2416ece7053SIgor Mammedov uint32_t addr_max, uint32_t addr_trans, 2426ece7053SIgor Mammedov uint32_t len); 2436ece7053SIgor Mammedov Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed, 244ff80dc7fSShannon Zhao AmlMaxFixed max_fixed, AmlCacheable cacheable, 2456ece7053SIgor Mammedov AmlReadAndWrite read_and_write, 2466ece7053SIgor Mammedov uint64_t addr_gran, uint64_t addr_min, 2476ece7053SIgor Mammedov uint64_t addr_max, uint64_t addr_trans, 2486ece7053SIgor Mammedov uint64_t len); 2493c054bd5SIgor Mammedov 2502ef7c27bSIgor Mammedov /* Block AML object primitives */ 2512ef7c27bSIgor Mammedov Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 252be06ebd0SIgor Mammedov Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 253ea2407d7SIgor Mammedov Aml *aml_method(const char *name, int arg_count); 25432acac9eSIgor Mammedov Aml *aml_if(Aml *predicate); 2553bfa74a7SIgor Mammedov Aml *aml_package(uint8_t num_elements); 256ed8b5847SShannon Zhao Aml *aml_buffer(int buffer_size, uint8_t *byte_list); 257ad4a80bcSIgor Mammedov Aml *aml_resource_template(void); 258af509897SZhu Guihua Aml *aml_field(const char *name, AmlAccessType type, AmlUpdateRule rule); 259a678508eSIgor Mammedov Aml *aml_varpackage(uint32_t num_elements); 260*b930fb9dSShannon Zhao Aml *aml_touuid(const char *uuid); 2612ef7c27bSIgor Mammedov 262658c2718SShannon Zhao void 263658c2718SShannon Zhao build_header(GArray *linker, GArray *table_data, 264658c2718SShannon Zhao AcpiTableHeader *h, const char *sig, int len, uint8_t rev); 265658c2718SShannon Zhao void *acpi_data_push(GArray *table_data, unsigned size); 266658c2718SShannon Zhao unsigned acpi_data_len(GArray *table); 267658c2718SShannon Zhao void acpi_add_table(GArray *table_offsets, GArray *table_data); 268658c2718SShannon Zhao void acpi_build_tables_init(AcpiBuildTables *tables); 269658c2718SShannon Zhao void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre); 270243bdb79SShannon Zhao void 271243bdb79SShannon Zhao build_rsdt(GArray *table_data, GArray *linker, GArray *table_offsets); 272658c2718SShannon Zhao 27319934e0eSIgor Mammedov #endif 274