xref: /openbmc/qemu/include/hw/acpi/aml-build.h (revision 658c27181bf3b08a9cf2fab00ecce7f76065f6af)
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"
7*658c2718SShannon Zhao #include "hw/acpi/acpi-defs.h"
8*658c2718SShannon Zhao 
9*658c2718SShannon Zhao /* Reserve RAM space for tables: add another order of magnitude. */
10*658c2718SShannon Zhao #define ACPI_BUILD_TABLE_MAX_SIZE         0x200000
11*658c2718SShannon Zhao 
12*658c2718SShannon Zhao #define ACPI_BUILD_APPNAME  "Bochs"
13*658c2718SShannon Zhao #define ACPI_BUILD_APPNAME6 "BOCHS "
14*658c2718SShannon Zhao #define ACPI_BUILD_APPNAME4 "BXPC"
15*658c2718SShannon Zhao 
16*658c2718SShannon Zhao #define ACPI_BUILD_TABLE_FILE "etc/acpi/tables"
17*658c2718SShannon Zhao #define ACPI_BUILD_RSDP_FILE "etc/acpi/rsdp"
18*658c2718SShannon 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 {
3952fa397cSIgor Mammedov     aml_decode10 = 0,
4052fa397cSIgor Mammedov     aml_decode16 = 1,
4152fa397cSIgor Mammedov } AmlIODecode;
4252fa397cSIgor Mammedov 
4331127938SIgor Mammedov typedef enum {
44e2ea299bSIgor Mammedov     aml_any_acc = 0,
45214ae59fSIgor Mammedov     aml_byte_acc = 1,
46e2ea299bSIgor Mammedov     aml_word_acc = 2,
47e2ea299bSIgor Mammedov     aml_dword_acc = 3,
48e2ea299bSIgor Mammedov     aml_qword_acc = 4,
49e2ea299bSIgor Mammedov     aml_buffer_acc = 5,
50214ae59fSIgor Mammedov } AmlFieldFlags;
51214ae59fSIgor Mammedov 
52214ae59fSIgor Mammedov typedef enum {
5331127938SIgor Mammedov     aml_system_memory = 0x00,
5431127938SIgor Mammedov     aml_system_io = 0x01,
5531127938SIgor Mammedov } AmlRegionSpace;
5631127938SIgor Mammedov 
576ece7053SIgor Mammedov typedef enum {
586ece7053SIgor Mammedov     aml_memory_range = 0,
596ece7053SIgor Mammedov     aml_io_range = 1,
606ece7053SIgor Mammedov     aml_bus_number_range = 2,
616ece7053SIgor Mammedov } AmlResourceType;
626ece7053SIgor Mammedov 
636ece7053SIgor Mammedov typedef enum {
646ece7053SIgor Mammedov     aml_sub_decode = 1 << 1,
656ece7053SIgor Mammedov     aml_pos_decode = 0
666ece7053SIgor Mammedov } AmlDecode;
676ece7053SIgor Mammedov 
686ece7053SIgor Mammedov typedef enum {
696ece7053SIgor Mammedov     aml_max_fixed = 1 << 3,
706ece7053SIgor Mammedov     aml_max_not_fixed = 0,
716ece7053SIgor Mammedov } AmlMaxFixed;
726ece7053SIgor Mammedov 
736ece7053SIgor Mammedov typedef enum {
746ece7053SIgor Mammedov     aml_min_fixed = 1 << 2,
756ece7053SIgor Mammedov     aml_min_not_fixed = 0
766ece7053SIgor Mammedov } AmlMinFixed;
776ece7053SIgor Mammedov 
786ece7053SIgor Mammedov /*
796ece7053SIgor Mammedov  * ACPI 1.0b: Table 6-26 I/O Resource Flag (Resource Type = 1) Definitions
806ece7053SIgor Mammedov  * _RNG field definition
816ece7053SIgor Mammedov  */
826ece7053SIgor Mammedov typedef enum {
836ece7053SIgor Mammedov     aml_isa_only = 1,
846ece7053SIgor Mammedov     aml_non_isa_only = 2,
856ece7053SIgor Mammedov     aml_entire_range = 3,
866ece7053SIgor Mammedov } AmlISARanges;
876ece7053SIgor Mammedov 
886ece7053SIgor Mammedov /*
896ece7053SIgor Mammedov  * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions
906ece7053SIgor Mammedov  * _MEM field definition
916ece7053SIgor Mammedov  */
926ece7053SIgor Mammedov typedef enum {
936ece7053SIgor Mammedov     aml_non_cacheable = 0,
946ece7053SIgor Mammedov     aml_cacheable = 1,
956ece7053SIgor Mammedov     aml_write_combining = 2,
966ece7053SIgor Mammedov     aml_prefetchable = 3,
976ece7053SIgor Mammedov } AmlCacheble;
986ece7053SIgor Mammedov 
996ece7053SIgor Mammedov /*
1006ece7053SIgor Mammedov  * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions
1016ece7053SIgor Mammedov  * _RW field definition
1026ece7053SIgor Mammedov  */
1036ece7053SIgor Mammedov typedef enum {
1046ece7053SIgor Mammedov     aml_ReadOnly = 0,
1056ece7053SIgor Mammedov     aml_ReadWrite = 1,
1066ece7053SIgor Mammedov } AmlReadAndWrite;
1076ece7053SIgor Mammedov 
108*658c2718SShannon Zhao typedef
109*658c2718SShannon Zhao struct AcpiBuildTables {
110*658c2718SShannon Zhao     GArray *table_data;
111*658c2718SShannon Zhao     GArray *rsdp;
112*658c2718SShannon Zhao     GArray *tcpalog;
113*658c2718SShannon Zhao     GArray *linker;
114*658c2718SShannon Zhao } AcpiBuildTables;
115*658c2718SShannon Zhao 
1160f2707e4SIgor Mammedov /**
1170f2707e4SIgor Mammedov  * init_aml_allocator:
1180f2707e4SIgor Mammedov  *
1190f2707e4SIgor Mammedov  * Called for initializing API allocator which allow to use
1200f2707e4SIgor Mammedov  * AML API.
1210f2707e4SIgor Mammedov  * Returns: toplevel container which accumulates all other
1220f2707e4SIgor Mammedov  * AML elements for a table.
1230f2707e4SIgor Mammedov  */
1240f2707e4SIgor Mammedov Aml *init_aml_allocator(void);
1250f2707e4SIgor Mammedov 
1260f2707e4SIgor Mammedov /**
1270f2707e4SIgor Mammedov  * free_aml_allocator:
1280f2707e4SIgor Mammedov  *
1290f2707e4SIgor Mammedov  * Releases all elements used by AML API, frees associated memory
1300f2707e4SIgor Mammedov  * and invalidates AML allocator. After this call @init_aml_allocator
1310f2707e4SIgor Mammedov  * should be called again if AML API is to be used again.
1320f2707e4SIgor Mammedov  */
1330f2707e4SIgor Mammedov void free_aml_allocator(void);
1340f2707e4SIgor Mammedov 
1350f2707e4SIgor Mammedov /**
1360f2707e4SIgor Mammedov  * aml_append:
1370f2707e4SIgor Mammedov  * @parent_ctx: context to which @child element is added
1380f2707e4SIgor Mammedov  * @child: element that is copied into @parent_ctx context
1390f2707e4SIgor Mammedov  *
1400f2707e4SIgor Mammedov  * Joins Aml elements together and helps to construct AML tables
1410f2707e4SIgor Mammedov  * Examle of usage:
1420f2707e4SIgor Mammedov  *   Aml *table = aml_def_block("SSDT", ...);
1430f2707e4SIgor Mammedov  *   Aml *sb = aml_scope("\_SB");
1440f2707e4SIgor Mammedov  *   Aml *dev = aml_device("PCI0");
1450f2707e4SIgor Mammedov  *
1460f2707e4SIgor Mammedov  *   aml_append(dev, aml_name_decl("HID", aml_eisaid("PNP0A03")));
1470f2707e4SIgor Mammedov  *   aml_append(sb, dev);
1480f2707e4SIgor Mammedov  *   aml_append(table, sb);
1490f2707e4SIgor Mammedov  */
1500f2707e4SIgor Mammedov void aml_append(Aml *parent_ctx, Aml *child);
1510f2707e4SIgor Mammedov 
1523c054bd5SIgor Mammedov /* non block AML object primitives */
1533c054bd5SIgor Mammedov Aml *aml_name(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
1543c054bd5SIgor Mammedov Aml *aml_name_decl(const char *name, Aml *val);
155b25af5adSIgor Mammedov Aml *aml_return(Aml *val);
156295a515dSIgor Mammedov Aml *aml_int(const uint64_t val);
1577193f3a6SIgor Mammedov Aml *aml_arg(int pos);
158c263b3f7SIgor Mammedov Aml *aml_store(Aml *val, Aml *target);
159926f5aaeSIgor Mammedov Aml *aml_and(Aml *arg1, Aml *arg2);
16034189453SIgor Mammedov Aml *aml_notify(Aml *arg1, Aml *arg2);
1613f3992b7SIgor Mammedov Aml *aml_call1(const char *method, Aml *arg1);
1623f3992b7SIgor Mammedov Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2);
1633f3992b7SIgor Mammedov Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3);
1643f3992b7SIgor Mammedov Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4);
16552fa397cSIgor Mammedov Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base,
16652fa397cSIgor Mammedov             uint8_t aln, uint8_t len);
16731127938SIgor Mammedov Aml *aml_operation_region(const char *name, AmlRegionSpace rs,
16831127938SIgor Mammedov                           uint32_t offset, uint32_t len);
16970560453SIgor Mammedov Aml *aml_irq_no_flags(uint8_t irq);
170214ae59fSIgor Mammedov Aml *aml_named_field(const char *name, unsigned length);
171e2ea299bSIgor Mammedov Aml *aml_reserved_field(unsigned length);
172b8a5d689SIgor Mammedov Aml *aml_local(int num);
173d5e5830fSIgor Mammedov Aml *aml_string(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
17415e44e56SIgor Mammedov Aml *aml_equal(Aml *arg1, Aml *arg2);
1753dd15643SIgor Mammedov Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len,
1763dd15643SIgor Mammedov                    const char *name_format, ...) GCC_FMT_ATTR(4, 5);
177a7891dacSIgor Mammedov Aml *aml_eisaid(const char *str);
1786ece7053SIgor Mammedov Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
1796ece7053SIgor Mammedov                          AmlDecode dec, uint16_t addr_gran,
1806ece7053SIgor Mammedov                          uint16_t addr_min, uint16_t addr_max,
1816ece7053SIgor Mammedov                          uint16_t addr_trans, uint16_t len);
1826ece7053SIgor Mammedov Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
1836ece7053SIgor Mammedov                  AmlDecode dec, AmlISARanges isa_ranges,
1846ece7053SIgor Mammedov                  uint16_t addr_gran, uint16_t addr_min,
1856ece7053SIgor Mammedov                  uint16_t addr_max, uint16_t addr_trans,
1866ece7053SIgor Mammedov                  uint16_t len);
1876ece7053SIgor Mammedov Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed,
1886ece7053SIgor Mammedov                       AmlMaxFixed max_fixed, AmlCacheble cacheable,
1896ece7053SIgor Mammedov                       AmlReadAndWrite read_and_write,
1906ece7053SIgor Mammedov                       uint32_t addr_gran, uint32_t addr_min,
1916ece7053SIgor Mammedov                       uint32_t addr_max, uint32_t addr_trans,
1926ece7053SIgor Mammedov                       uint32_t len);
1936ece7053SIgor Mammedov Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed,
1946ece7053SIgor Mammedov                       AmlMaxFixed max_fixed, AmlCacheble cacheable,
1956ece7053SIgor Mammedov                       AmlReadAndWrite read_and_write,
1966ece7053SIgor Mammedov                       uint64_t addr_gran, uint64_t addr_min,
1976ece7053SIgor Mammedov                       uint64_t addr_max, uint64_t addr_trans,
1986ece7053SIgor Mammedov                       uint64_t len);
1993c054bd5SIgor Mammedov 
2002ef7c27bSIgor Mammedov /* Block AML object primitives */
2012ef7c27bSIgor Mammedov Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
202be06ebd0SIgor Mammedov Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
203ea2407d7SIgor Mammedov Aml *aml_method(const char *name, int arg_count);
20432acac9eSIgor Mammedov Aml *aml_if(Aml *predicate);
2053bfa74a7SIgor Mammedov Aml *aml_package(uint8_t num_elements);
20604b8da54SIgor Mammedov Aml *aml_buffer(void);
207ad4a80bcSIgor Mammedov Aml *aml_resource_template(void);
208214ae59fSIgor Mammedov Aml *aml_field(const char *name, AmlFieldFlags flags);
209a678508eSIgor Mammedov Aml *aml_varpackage(uint32_t num_elements);
2102ef7c27bSIgor Mammedov 
211*658c2718SShannon Zhao void
212*658c2718SShannon Zhao build_header(GArray *linker, GArray *table_data,
213*658c2718SShannon Zhao              AcpiTableHeader *h, const char *sig, int len, uint8_t rev);
214*658c2718SShannon Zhao void *acpi_data_push(GArray *table_data, unsigned size);
215*658c2718SShannon Zhao unsigned acpi_data_len(GArray *table);
216*658c2718SShannon Zhao void acpi_add_table(GArray *table_offsets, GArray *table_data);
217*658c2718SShannon Zhao void acpi_build_tables_init(AcpiBuildTables *tables);
218*658c2718SShannon Zhao void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre);
219*658c2718SShannon Zhao 
22019934e0eSIgor Mammedov #endif
221